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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

read [-r] [-p PROMPT] [-a ARRAY] [-d DELIM] [-t TIMEOUT] [NAME...]

Parameters

ParameterDescription
-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

Read fields line by line
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.

Split a string into an array
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.

Was this page helpful?