LinuxCommandLibrary

mapfile

TLDR

Read file into array

$ mapfile [array] < [file.txt]
copy
Read with line limit
$ mapfile -n [10] [array] < [file.txt]
copy
Skip first N lines
$ mapfile -s [2] [array] < [file.txt]
copy
Remove trailing newlines
$ mapfile -t [array] < [file.txt]
copy
Use specific delimiter
$ mapfile -d ':' [array] < [file.txt]
copy
Read from command
$ mapfile [array] < <(ls)
copy

SYNOPSIS

mapfile [options] [array]

DESCRIPTION

mapfile reads lines into a bash array. It's a bash builtin for array population.
The tool is also known as readarray. It efficiently reads files line by line into arrays.
mapfile reads lines into array.

PARAMETERS

ARRAY

Array variable name.
-n COUNT
Maximum lines to read.
-s COUNT
Lines to skip.
-t
Remove trailing delimiters.
-d DELIM
Use delimiter instead of newline.

CAVEATS

Bash-specific builtin. Not available in sh. Alias is readarray.

HISTORY

mapfile was added to Bash 4.0 as a builtin for efficiently reading files into arrays.

SEE ALSO

readarray(1), read(1), bash(1)

Copied to clipboard