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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

mapfile [-t] [-n COUNT] [-s SKIP] ARRAY

Parameters

ParameterDescription
-t Remove the trailing newline from each line.
-n COUNT Read at most COUNT lines.
-s SKIP Skip the first SKIP lines.

Examples

Lines into an array, skipping a header
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.

From a command with process substitution
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.

Was this page helpful?