Bash 5 reference · Shell Builtins and Syntax
read (Bash builtin)
Read a line from standard input into variables, splitting on IFS; use -r to keep backslashes and -p for a prompt.
Bash 5
Syntax
read [-r] [-p PROMPT] [-a ARRAY] [-d DELIM] [-t TIMEOUT] [NAME...]
Parameters
| Parameter | Description |
|---|---|
-r |
Don't treat backslashes as escapes (almost always wanted). |
-a ARRAY |
Split the line into an array. |
-t SECONDS |
Give up after a timeout. |
IFS= |
Set before read to keep leading and trailing spaces. |
Examples
printf 'web-1 10.0.0.11\ndb-1 10.0.0.21\n' > hosts
while read -r name ip; do echo "$name is at $ip"; done < hosts
Output
web-1 is at 10.0.0.11 db-1 is at 10.0.0.21
Run this on your computer — see “Get Started” for setup.
IFS=, read -r -a parts <<< 'a,b,c'
echo "${#parts[@]} parts, second=${parts[1]}"
Output
3 parts, second=b
Run this on your computer — see “Get Started” for setup.
Related
mapfile· Read all lines of input into an indexed array at once (also called readarray), optionally trimming newlines with -t.while· Repeat commands while a condition succeeds; with read, it's the standard way to process input line by line.- Bash Loops · tutorial