mapfile
The bash 4 builtin, also called readarray, that reads every line of its input into an array in one step.
In more detail
mapfile -t lines < file drops the newline from each element. Feeding it from a redirect or process substitution, not a pipe, keeps the array in the current shell. It is missing from bash 3.2 and POSIX sh.
Example
mapfile -t lines < config.env; echo "${#lines[@]}"Related terms
- shell array — A variable that holds a numbered list of separate values, written in bash as names=(one "two words" three).
- read builtin — The bash builtin that reads one line of input and stores it, or its fields, in variables.
- process substitution — The bash form <(command), which runs a command and hands its output to another program as if it were a file name.
Taught in
- Which Shell Am I In, And Getting Bash Five — Bash Scripting & Command Line Automation
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- POSIX sh Versus Bash — Bash Scripting & Command Line Automation