mapfile Command in Linux With Examples
Last Updated :
05 Sep, 2024
The 'mapfile' command, also known as 'readarray', is a powerful built-in feature of the Bash shell used for reading input lines into an array variable. Unlike using a loop with read, mapfile reads lines directly from standard input or command substitution (< <(command)) rather than from a pipe, making it faster and more convenient for handling arrays. If no array name is provided, 'mapfile' defaults to using the variable MAPFILE to store the data.
What does the 'mapfile' Command do?
The 'mapfile' command is particularly useful when you need to read data line by line into an array, allowing you to manipulate each line individually or collectively with ease. This command is especially beneficial when dealing with data processing, file reading, and output capturing tasks in scripts.
Syntax: mapfile [array]
Alternatively, we can use read array [arrayname] instead of mapfile.
mapfile Command Examples in Linux
Example 1. Reading an array from a file:
$ mapfile MYFILE < example.txt
$ echo ${MYFILE[@]}
$ echo ${MYFILE[0]}
Output:

Example 2. Capture the output into an array:
$ mapfile GEEKSFORGEEKS < <(printf "Item 1\nItem 2\nItem 3\n")
$ echo ${GEEKSFORGEEKS[@]}
Here, Item1, Item2, and Item 3 have been stored in the array GEEKSFORGEEKS.
Output:

Example 3. Strip newlines and store item using -t:
$ mapfile -t GEEKSFORGEEKS< <(printf "Item 1\nItem 2\nItem 3\n")
$ printf "%s\n" "${GEEKSFORGEEKS[@]}"
Output:

Example 4. Read the specified number of lines using -n:
$ mapfile -n 2 GEEKSFORGEEKS < example.txt
$ echo ${GEEKSFORGEEKS[@]}
It reads at most 2 lines. If 0 is specified then all lines are considered.
Output:

Conclusion
The mapfile command in Bash is a useful tool for reading lines into arrays from standard input or command substitution. It makes handling data in scripts easier by eliminating the need for loops and provides various options for managing input, like stripping newlines, reading a set number of lines, or using callbacks for processing. Mapfile and its options can enhance your data handling capabilities in Bash, leading to more powerful and flexible scripting.
Similar Reads
pmap command in Linux with Examples The pmap command in Linux is a powerful utility used to display the memory map of a process. A memory map provides insight into how memory is allocated and distributed within a running process. This can be incredibly useful for developers and system administrators when debugging memory issues, optim
3 min read
ln command in Linux with Examples The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read
Linux make Command with Examples The make command for Linux is a very useful utility in the automation of software development and performing tasks in a Linux environment. It simply reads a special file, which is called a Makefile and this file describes how one's program is compiled and linked with another file or another program
6 min read
rcp Command in Linux with examples When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginn
5 min read
tac command in Linux with Examples tac command in Linux is used to concatenate and print files in reverse. This command will write each FILE to standard output, the last line first. When no file is specified then this command will read the standard input. Here, we will look deeper into the tac command, exploring its syntax, various o
3 min read
whatis Command in Linux with Examples whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument. Syntax of the `whatis` command
5 min read