Shell
mapfile
Read lines from stdin into an indexed array variable.
shellarrayreadfilebash-builtin
Additional Notes
mapfile (also called readarray) is a Bash built-in that reads lines from standard input into an indexed array. Each line of input becomes one element of the array, stripping the trailing newline.
It is useful for reading file contents or command output into an array for further processing in scripts. It is more efficient and cleaner than using a loop with read.
Syntax
mapfile [options] array_name
mapfile -t array_name < file
Parameters
array_name: The name of the array variable to populate.
Common Options
-t: Remove trailing newlines from each line (most commonly used).-n count: Read at mostcountlines.-O origin: Start filling the array at indexorigin(default 0).-s count: Skip the firstcountlines.-C callback: Evaluatecallbackevery-clines.-c count: Specify the callback frequency (default is 5000).-u fd: Read from file descriptorfdinstead of stdin.
Examples
mapfile -t lines < /etc/passwd
echo "${lines[0]}"
Read /etc/passwd into an array and print the first line.
mapfile -t -s 1 lines < /etc/fstab
Skip the first line (header comment) and read the rest.
mapfile -t -n 3 lines < /etc/hosts
Read only the first 3 lines.
ls -1 *.txt | mapfile -t files
for file in "${files[@]}"; do
echo "Processing: $file"
done
Read ls output into an array and iterate.
mapfile -t -C 'echo "Read line"' -c 1 lines < data.txt
Execute a callback after each line read.
Practical Notes
readarrayis a synonym formapfile; both work identically.- Always use
-tto strip trailing newlines unless you need them. mapfileis a Bash 4+ feature. It is not available in POSIXshor older Bash versions.- For large files,
mapfileis more memory-efficient than reading into a variable with$(cat).