read builtin
The bash builtin that reads one line of input and stores it, or its fields, in variables.
In more detail
Always use -r, or backslashes in the input are treated as escapes and disappear. Put IFS= in front to keep leading and trailing spaces, and redirect the file after done so a while loop reads it line by line. -p shows a prompt and -a reads the fields into an array.
Examples
read -r -p "Your name: " namewhile IFS='=' read -r key value; do echo "$key"; done < config.envRelated terms
- IFS — The internal field separator: a shell variable listing the characters that word splitting and read cut text at.
- while loop — A loop that keeps repeating its block as long as a condition stays true, checking the condition before each pass.
- mapfile — The bash 4 builtin, also called readarray, that reads every line of its input into an array in one step.
- here string — The bash form <<< word, which feeds a single string to a command as its standard input.
Taught in
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Pipelines, tee And Process Substitution — Bash Scripting & Command Line Automation
- Command Substitution And Subshells — Bash Scripting & Command Line Automation
- Script Input And Output: read, Heredocs, printf — Bash Scripting & Command Line Automation