Linux Commands

Mapfile Bash Linux Command

The bash shell Mapfile command is often known as a reading array. The primary purpose is to read standard input lines and store them in an indexed array variable. Mapfile must also read from substitution (<<) rather than a pipe. In addition, as compared to a read loop, bash Mapfile is a much faster and more convenient solution. It returns 1 if the execution of the command gets successful and 0 in case it is not successful. If we don’t specify an array name, the bash Mapfile variable will be targeting the array variable by default. Thus, we have decided to cover some examples using the mapfile instruction on the bash.

The touch command is used to create, change, and edit timestamps on files in the UNIX/Linux operating system. So, as shown in the image, we have tried the “touch” instruction within our shell to create a new text file. GNU nano is a basic built-in command-line editor for Unix/Linux operating systems. Type “nano” after the $ symbol, followed by the file’s name to open or create a new file.

$ touch new.txt
$ nano new.txt

Example 1: Read From File As Array

The “nano” command in the shell opens the “new.txt” file in the “nano” editor. We have typed the text One, TWo, and THree in the text file in three separate lines as shown. Save this simple text data file via the Ctrl+S and close the “nano” editor via the Ctrl+X shortcut.

It’s time to use the mapfile instruction to read the just added text data into an array form. The text lines in the new.txt file are stored as the array elements in the variable “var” using the mapfile command. In the second line, the echo query is used to print all of the data on each index of an array stored in the variable “var” in an array format. Similarly, the next lines of “echo” statement codes are used to display data at a specific index, i.e., 0,1,2,3 of an array separately. As the array contains only 3 values, thus the result of index 3 returns nothing.

$ mapfile Var < new.txt
$ echo ${Var[@]}
$ echo ${Var[0]}
$ echo ${Var[1]}
$ echo ${Var[2]}
$ echo ${Var[3]}

Example 2: Read output data into an array

We can also make use of some output statement data from a bash instruction to the array using the mapfile instruction. We have been using the printf statement to get its output data into an array “Arr.” Three values, “Ana,” “Paul,” and “George,” separated by line break special character “\n” will be stored in it using the mapfile instruction. Following that, the first echo command is used to display all of the data in an array. The data that occurs on each index of an array “Arr” is displayed similarly in the next lines of code via the “echo” statement followed by the index numbers in the square brackets. Because there is no data on the third index of an array, no output is displayed on the screen. Let’s move toward the next example.

$ mapfile Arr < <(printf “Ana \nPaul \n George\n”)
$ echo ${Var[@]}
$ echo ${Var[0]}
$ echo ${Var[1]}
$ echo ${Var[2]}
$ echo ${Var[3]}
$ echo ${Var[0]}

Example 03: Mapfile –n Option

The map file command has built-in options for execution. The -n option has been utilized to specify the number of values from the data file to take as an input for an array. All of the lines should be copied into an array when the value of n is zero. The first line in the example below uses the map file command with the –n option set to 2. This means it will read the first two lines from the file into an array called “Var” as index values for an array.

The echo command displays data on all indexes of the array “Var,” i.e., 2 indexes, implying that the map file command copies the first two lines from the file with the –n option. Using the “-n” option with value 1 in the mapfile instruction will only read a single line from the file, as shown below.

$ mapfile –n Var < new.txt
$ echo ${Var[@]}
$ mapfile –n 1 Var < new.txt
$ echo ${Var[@]}

Example 04: Mapfile –t Option

The “-t” option is used to strip/remove newlines from an item by storing it in an array. The example below shows the practical execution of command –t. The substitution operator (<) is used to populate the data into an array named “Arr.” The Mapfile instruction receives standard input strings, i.e., “Ana,” “Paul,” and “George,” from the printf statement and appends newlines (-t) to each line. When we run the entire command, mapfile silently scans the text lines and places each line into its specific array variable. The use of the printf statement is simply showing us that the string elements have been considered as array elements while “\n” has been used to give the line break in the shell.

$ mapfile -T Arr< <(printf “Ana \nPaul \nGeorge\n”)
$ printf “%s\n”  “${Arr[@]}”

The examples below show how to print the individual items of an array using indexes.

$ printf “%s\n”  “${Arr[0]}”
$ printf “%s\n”  “${Arr[1]}”
$ printf “%s\n”  “${Arr[2]}”
$ printf “%s\n”  “${Arr[3]}”

In the example below, the echo command prints all the elements of the array variable MAPFILE, separated by a space.

$ printf “Ana\nPaul\nGeorge\n” | (mapfile; echo “${MAPFILE[@]}”)

We may fix it and remove the line breaks by using the -t option of mapfile instruction.

$ printf “Ana\nPaul\nGeorge\n” | (mapfile -t; echo “${MAPFILE[@]}”)

Conclusion

This article was all about the utilization of bash mapfile instruction on the Ubuntu 20.04 system to take any input text data from an array within some variable and display it according to that. We have tried the mapfile instruction with “-n” and “-t” options to get a different format of outputs for arrays at the bash shell.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.