Using Bash mapfile to Load Files into Arrays Efficiently
Learn how Bash’s mapfile builtin loads files into arrays faster and cleaner than traditional while-read loops, with a concrete example, trade‑offs, and verification steps.
14 Mar 2026, 00:24 UTC

The problem with while-read loops
Reading a file line‑by‑line with a while read loop often looks like this:
while IFS= read -r line; do
# process "$line"
done < input.txt
Although functional, the loop spawns a subshell when the redirection is piped, and you must remember to set IFS= to preserve leading/trailing whitespace. Empty lines are dropped unless you tweak the delimiter, and trailing newlines are kept unless you strip them manually.
Introducing mapfile (readarray)
Bash 4.0 added the builtin mapfile (also aliased as readarray) that reads standard input directly into an indexed array.
The simplest form:
mapfile -t my_array < input.txt
The -t option removes the trailing newline from each element, giving you clean strings ready for further processing.
Worked example: skipping a CSV header
Suppose you have a CSV file data.csv where the first line is a header you want to ignore, and you need the remaining rows as array elements.
# Ensure you are using Bash 4.0 or newer
bash --version # look for 4.x or higher
# Read the file, skip the first line, strip newlines
mapfile -t -s 1 rows < data.csv
# Verify the result
echo "Number of rows: ${#rows[@]}"
echo "First row: ${rows[0]}"
Here -s 1 tells mapfile to skip one line before storing data. No subshell is created because the redirection happens in the current shell process.
Trade‑offs and limits
- Memory usage: The entire file is loaded into RAM. For files larger than a few hundred megabytes you may hit an OOM condition; test with
/usr/bin/time -v mapfile -t arr < hugefileand check the "Maximum resident set size". - Bash version: Requires Bash 4.0+. On systems where
/bin/shpoints to dash, the builtin will not be available. - Input source: mapfile reads from stdin, so you must redirect a file or pipe output into it.
If you need to process a truly massive file line‑by‑line without loading it all, revert to a while read loop or use streaming tools like awk.
Actionable closing
When you control the environment and the file size is modest, replace manual while read loops with mapfile -t for clearer code and fewer process forks. Verify your Bash version, run the command, and check the array length to confirm success.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.