Mapfile Bash Linux Command


The bash shell Mapfile command is frequently referred to as a studying array. The number one objective is to learn same old enter strains and retailer them in an listed array variable. Mapfile should additionally learn from substitution (

The contact command is used to create, alternate, and edit timestamps on recordsdata within the UNIX/Linux working gadget. So, as proven within the symbol, we’ve attempted the “touch” instruction inside our shell to create a brand new textual content document. GNU nano is a fundamental integrated command-line editor for Unix/Linux working methods. Type “nano” after the $ image, adopted via the document’s title to open or create a brand new document.

$ contact new.txt
$ nano new.txt

Example 1: Read From File As Array

The “nano” command within the shell opens the “new.txt” document within the “nano” editor. We have typed the textual content One, TWo, and THree within the textual content document in 3 separate strains as proven. Save this straightforward textual content information document by the use of the Ctrl+S and shut the “nano” editor by the use of the Ctrl+X shortcut.

It’s time to make use of the mapfile instruction to learn the simply added textual content information into an array shape. The textual content strains within the new.txt document are saved because the array components within the variable “var” the use of the mapfile command. In the second one line, the echo question is used to print all the information on every index of an array saved within the variable “var” in an array layout. Similarly, the following strains of “echo” commentary codes are used to show information at a particular index, i.e., 0,1,2,3 of an array one at a time. As the array accommodates best 3 values, thus the results of index 3 returns not anything.

$ mapfile Var $ echo ${Var[@]}
$ echo ${Var[0]}
$ echo ${Var[1]}
$ echo ${Var[2]}
$ echo ${Var[3]}

Example 2: Read output information into an array

We too can make use of a few output commentary information from a bash instruction to the array the use of the mapfile instruction. We had been the use of the printf commentary to get its output information into an array “Arr.” Three values, “Ana,” “Paul,” and “George,” separated via line spoil particular personality “n” will probably be saved in it the use of the mapfile instruction. Following that, the primary echo command is used to show all the information in an array. The information that happens on every index of an array “Arr” is displayed in a similar fashion within the subsequent strains of code by the use of the “echo” commentary adopted via the index numbers within the sq. brackets. Because there is not any information at the 3rd index of an array, no output is displayed at the display. Let’s transfer towards the following instance.

$ mapfile Arr $ echo ${Var[@]}
$ echo ${Var[0]}
$ echo ${Var[1]}
$ echo ${Var[2]}
$ echo ${Var[3]}
$ echo ${Var[0]}

Example 03: Mapfile –n Option

The map document command has integrated choices for execution. The -n possibility has been applied to specify the choice of values from the information document to take as an enter for an array. All of the strains will have to be copied into an array when the worth of n is 0. The first line within the instance under makes use of the map document command with the –n possibility set to two. This way it’s going to learn the primary two strains from the document into an array known as “Var” as index values for an array.

The echo command shows information on all indexes of the array “Var,” i.e., 2 indexes, implying that the map document command copies the primary two strains from the document with the –n possibility. Using the “-n” possibility with worth 1 within the mapfile instruction will best learn a unmarried line from the document, as proven under.

$ mapfile –n Var $ echo ${Var[@]}
$ mapfile –n 1 Var $ echo ${Var[@]}

Example 04: Mapfile –t Option

The “-t” possibility is used to strip/take away newlines from an merchandise via storing it in an array. The instance under presentations the sensible execution of command –t. The substitution operator (

$ mapfile -T Arr$ printf “%sn”  “${Arr[@]}”

The examples under display the right way to print the person pieces of an array the use of indexes.

$ printf “%sn”  “${Arr[0]}”
$ printf “%sn”  “${Arr[1]}”
$ printf “%sn”  “${Arr[2]}”
$ printf “%sn”  “${Arr[3]}”

In the instance under, the echo command prints the entire components of the array variable MAPFILE, separated via an area.

$ printf “AnanPaulnGeorgen” | (mapfile; echo “${MAPFILE[@]}”)

We might repair it and take away the road breaks via the use of the -t possibility of mapfile instruction.

$ printf “AnanPaulnGeorgen” | (mapfile -t; echo “${MAPFILE[@]}”)


This article was once all in regards to the usage of bash mapfile instruction at the Ubuntu 20.04 gadget to take any enter textual content information from an array inside some variable and show it in keeping with that. We have attempted the mapfile instruction with “-n” and “-t” choices to get a distinct layout of outputs for arrays on the bash shell.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More