Bash 5 reference · Shell Builtins and Syntax
mapfile (Bash builtin)
Read all lines of input into an indexed array at once (also called readarray), optionally trimming newlines with -t.
Bash 5
Syntax
mapfile [-t] [-n COUNT] [-s SKIP] ARRAY
Parameters
| Parameter | Description |
|---|---|
-t |
Remove the trailing newline from each line. |
-n COUNT |
Read at most COUNT lines. |
-s SKIP |
Skip the first SKIP lines. |
Examples
printf 'id,name\n1,Ana\n2,Ben\n' > users.csv
mapfile -t -s 1 rows < users.csv
echo "${#rows[@]} rows; first: ${rows[0]}"
Output
2 rows; first: 1,Ana
Run this on your computer — see “Get Started” for setup.
mapfile -t files < <(printf '%s\n' b.txt a.txt c.txt | sort)
printf '[%s]\n' "${files[@]}"
Output
[a.txt] [b.txt] [c.txt]
Run this on your computer — see “Get Started” for setup.
Related
read· Read a line from standard input into variables, splitting on IFS; use -r to keep backslashes and -p for a prompt.arrays· Indexed and associative arrays: create, append, read elements, count, slice and loop over them safely.- Bash Arrays and Associative Arrays · tutorial