Employing mapfile command in Linux, you can instruct your system to read lines from the standard input into an indexed array variable. The mapfile
, also known as read array, is a Bash shell command invoked to read arrays.
The mapfile
command is not very portable. For that reason, invoking this command is not recommended when you want to make sure you can run your script on a wide array of systems. In this article, we’ll learn about using the mapfile
command and look at various options that come alongside it.
mapfile Command in Linux: Syntax
The basic structure of mapfile command in Linux looks something like this:
mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback [-c quantum]] [array]
Using mapfile Command in Linux
In case you don’t know, the mapfile
command basically puts each line in an array variable. The command reads the input line by line before working on it. Let’s use an example to understand this better.
To begin with, we use the printf
command to print text with multiple newlines. The string format includes \n
flag, which instructs the system to create a new line.
Suppose we want to create 5 lines, Line 1, Line 2, Line 3, Line 4, and Line 5. In that case, we run the following command:
printf "Line 1\nLine 2\nLine 3\nLine 4\nLine 5"
Now it is time we invoke the mapfile command in Linux and use it to put each of these five created lines in its own element of an array. Talking about the default scenario, mapfile
intends to read from standard input, so to ensure you pipe the output of printf, run the command like this:
printf "Line 1\nLine 2\nLine 3\nLine 4\nLine 5" | mapfile
Checking the Value of MAPFILE
Once done, you would expect the default array variable called, MAPFILE
to contain all the values from these lines. To check the value of each MAPFILE
, all that you need to do is run the following command:
echo "${MAPFILE[@]}”
mapfile Value Retuning Blank: The Reason
When you check the value of each MAPFILE
, you’ll get a blank line in most cases. Now the blank line indicates the variable is actually empty, so why is it so?
No matter which command in a pipeline we consider, it invokes within a subshell. For instance, the bash usually gets executed as a child process. Again, each subshell packs its own environment alongside a specific lexical scope.
The whole point is that the variables in a pipeline make each subshell’s environment not carry over to others. In other words, there resides no such thing called the environmental side effects, which are shared within elements.
Coming back to the situation above, the invoked mapfile
worked correctly and set the corresponding values of the MAPFILE
, but the same vanishes when we terminate the command’s subshell.
So what we need to do is, echo the value of MAPFILE
inside a subshell by simply enclosing both in parentheses. Here is how it is done:
printf "Line 1\nLine 2\nLine 3\nLine 4\nLine 5" | ( mapfile; echo "${MAPFILE[@]}" )
Line 1 Line 2 Line 3 Line 4 Line 5
A quick look at the command above will make you realize that the echo prints all the elements inside the array variable, MAPFILE
separated by a space.
The space we’re talking about appears at the very beginning of lines 2, 3, 4, and 5 because of the new lines in our data. To fix the line breaks, you need to strip them by passing the -t
flag.
printf Line 1\nLine 2\nLine 3\n Line4\n Line5\n | ( mapfile -t; echo "${MAPFILE[@]}" )
Line 1 Line 2 Line 3 Line 4 Line 5
Using mapfile Command in Linux with Process Substitution
Employing mapfile command in Linux alongside process substitution can help redirect the output to the mapfile without even using any pipeline.
mapfile -t < <(printf "Line 1\nLine 2\nLine 3\nLine4\nLine5")
Here, the mapfile
-t
flag takes the input from standard input and then strips newlines from the end of each line. In simple words, only the text of the line gets stored in the array element, while the newline character is discarded.
The first <
that we used in the command above is the redirection character. Usually, it is followed by a filename or any kind of file descriptor. The symbol works to redirect the contents of the file to the standard input. The standard input here is that of the preceding command.
mapfile Command: Options
-n
count: This option reads a maximum of count lines. When the count is zero, all the available lines are copied.
-O
origin: Using this option will simply begin the writing lines to the array at index number origin. By default, the value is zero.
-s
count: This option does discard the first count lines right before writing it to the array.
-t
: In case a line ends in a new line, use the t
option for stripping it.
-u fd
: This option reads lines from the file descriptor, fd
instead of standard input.
-C callback
: With this, you can Execute or evaluate any function, expression, or callback every time the quantum lines are read. By default, the value of quantum is set to 1 unless you specify it with -c
.
-c quantum
: You can specify the number of lines, quantum using the c
flag. It will help you define the number after which you want to execute or evaluate the function or expression callback.
Printing mapfile Command Help Status
Input:
$ mapfile --help
Output:
Mapfile Exit Status
The mapfile
command in Linux returns 0
when executed successfully. On the other hand, if anything, it returns 1
. The error reasons can range from providing an invalid option to targeting a variable that is read-only or not an array.
This is everything about the mapfile command in Linux. In this article, we’ve discussed the various uses of mapfile string and its corresponding options by presenting easy-to-understand examples.