io

read

Reads a single line from stdin (or a file descriptor) and splits it into the given variables using IFS. The workhorse for line-by-line input processing and interactive prompts.

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

Common flags / operators

Flag / Operator Purpose
-r Raw mode — don't interpret backslashes (always use this)
-p PROMPT Print PROMPT before reading
-a ARRAY Split words into indexed array ARRAY
-t SECONDS Timeout after N seconds (fractional allowed)
-s Silent — don't echo input (passwords)
-n N Return after N characters instead of a full line

Examples

read -rp 'Name: ' name

Prompt and read one line into $name.

while IFS=: read -r user _ uid _; do echo "$user $uid"; done < /etc/passwd

Split /etc/passwd on colons per line.

read -rsp 'Password: ' pw; echo

Silent password prompt; trailing echo adds a newline.

read -ra parts <<< "$line"

Here-string splits $line into an array using IFS.

Gotcha

Without -r, backslashes are treated as line-continuation and escape processing — almost always wrong. IFS is applied on the read, so 'IFS= read -r line' preserves leading/trailing whitespace.

Related

← All Bash builtins · Linux commands