Read all lines of a file into an array.

Section: Reading Input

Load file into array

bash
bash
mapfile -t lines < file.txt
Explanation

mapfile and readarray are Bash builtins.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Reading Input
Prompt user for input
Read one line from stdin with a prompt.
OpenIn sheetbashsame section
Read secret silently
Read secret input without echoing characters.
OpenIn sheetbashsame section
Read delimited fields
Parse delimited input into multiple variables.
OpenIn sheetbashsame section
Create indexed array
Create an indexed array.
OpenIn sheetbash1 tag match
Create associative array
Declare an associative array.
OpenIn sheetbash1 tag match
Access array element
Read an array element by index.
OpenIn sheetbash1 tag match