2024-10-19
mapfile?mapfile is a Bash built-in command that reads lines from a file or standard input and stores them into an array variable. This simplifies the process of loading data into arrays, eliminating the need for complex loops and manual array indexing. It offers flexibility and control over how data is imported, making it useful for various scripting tasks.
The simplest form of mapfile reads lines from standard input and assigns them to an array called myArray:
mapfile myArrayNow, type some lines of text, pressing Enter after each one. Once you’ve finished, press Ctrl+D (EOF) to signal the end of input. The lines you typed will be stored in myArray. You can access individual elements using array indexing:
echo "${myArray[0]}" # Prints the first line
echo "${myArray[1]}" # Prints the second line
echo "${#myArray[@]}" # Prints the number of elements in the arrayInstead of standard input, you can specify a file to read from using the -t option (to remove trailing newlines) and the < redirection operator:
mapfile -t myArray < myfile.txtThis reads each line from myfile.txt, removes the trailing newline character from each line and assigns the result to the myArray array.
Example myfile.txt:
Line one
Line two
Line three
After running the command, myArray[0] would hold “Line one”, myArray[1] would hold “Line two”, and so on.
While mapfile defaults to using the name myArray, you can explicitly specify the array variable name:
mapfile -t myData < myfile.txtThis will populate the myData array instead of myArray.
The -O option lets you specify the starting index for the array:
mapfile -t -O 3 myArray < myfile.txtThis starts the array index at 3, so the first line from myfile.txt will be stored in myArray[3], the second in myArray[4], and so on.
Use the -n option to limit the number of lines read:
mapfile -n 2 -t myArray < myfile.txtThis reads only the first two lines from myfile.txt into myArray.
By default, mapfile uses newline characters as separators. However, you can use the -d option to specify a different delimiter:
mapfile -d ',' -t myArray < myfile.csvThis reads lines from myfile.csv, using a comma as the delimiter. Each comma-separated value will become a separate element in the myArray array.
Example myfile.csv:
apple,banana,orange
grape,kiwi,mango
After running this, myArray[0] would be “apple”, myArray[1] would be “banana”, and so on.
You can combine multiple options for even more control. For example, to read the first 5 lines from a file, starting at index 2 and using a semicolon as the delimiter:
mapfile -n 5 -O 2 -d ';' -t myArray < myfile.txtIf your file contains empty lines, these will be included in the array as empty strings. You can filter these out if needed using additional scripting techniques.
Instead of a file, you can read from the output of a command using command substitution:
mapfile -t myArray < <(ls -l)This reads the output of the ls -l command and stores it in the myArray array. Note the use of process substitution <(command).
These examples demonstrate the versatility of mapfile. By mastering its options, you can efficiently manage data within your shell scripts, simplifying complex array manipulations.