read takes a line
The read builtin reads one line from standard input, removes the trailing newline, and stores the result in the variable you name. With no name, it stores the line in the special variable REPLY. With several names, read splits the line at the characters in IFS and gives each name one field. The last name takes the rest of the line, so extra words stay together.
read first last <<< "Ada Lovelace Byron"
echo "$first / $last"The here-string <<< feeds one string to the command as its input, with a newline added. The output is Ada / Lovelace Byron, because the last name collects everything after the first field.
Think of read as a function that returns one line per call. Each call consumes input, so the next call sees the line after it. This is why a loop around read processes every line exactly once, and why reading one line in a script leaves the remaining lines available to the next command in the same stream.
The read command exits with status 0 when it reads a line, and with status 1 at end of input. That status makes read a natural loop condition, which a later section uses.
By default read does two things that are often unwanted. It strips leading and trailing whitespace, using IFS. It also treats a backslash as an escape character, so a backslash at the end of a line joins that line to the next one. The -r option turns the escape processing off. That is almost always the right choice for input you did not write yourself.
Prompts and where input comes from
The -p option prints a prompt before reading. It writes the prompt to standard error, and it is useful only when a person is at a terminal. Scripts that process piped data should not depend on it. The -s option turns off echo, which suits a password. The -t option sets a timeout in seconds, and read returns a nonzero status when the time runs out.
Input can come from a terminal, a file, a pipe, or a here-string. Redirection such as read x < file reads one line from a file. The here-string form is handy for tests, because it needs no separate process. Remember that read takes only one line per call, so a script that needs several lines calls it again in a loop.
Reading from a prompt or a pipe
Interactive prompts and piped input look alike to read, but they differ in one way that matters. When standard input is a terminal, read waits for you to press Enter. When standard input is a pipe, read waits for the writer to send a newline or to close the pipe. A script that must work both ways should not depend on a prompt for its logic. Read the value from the arguments when it is available, and fall back to read only when it is missing. This keeps the script usable from a shell and from other programs.
Splitting into several variables
Give read more than one name when a line holds fields. The fields are split at the characters in IFS, which default to space, tab, and newline. To split at colons, as in the password file, set IFS for that one command, so the change does not reach the rest of the script:
line="alice:x:1001"
IFS=: read -r user _ uid <<< "$line"
echo "$uid $user"The name _ is a convention for a field you do not need. The shell gives it no special meaning, but it tells the reader that the value is discarded. The example prints 1001 alice.
The loop that reads every line
The standard way to process a file reads one line at a time in a while loop, with the file attached to the loop by a redirection at the end. Set IFS to an empty value for the read, and use -r, so each line arrives unchanged:
while IFS= read -r line; do
echo "got: $line"
done < input.txtThe last line of a file often has no trailing newline. The read command still stores that text, but it returns status 1, because it reached end of input before finding a newline. The loop therefore skips the final line unless the body also runs when the variable is not empty. The form while IFS= read -r line || [ -n "$line" ]; do handles both cases, and it is the form to use by default.
The pipeline trap
In bash, each part of a pipeline runs in its own process, unless the lastpipe option is enabled and job control is off. A while loop at the end of a pipe therefore changes its variables in a subshell, and those changes are gone when the pipe ends. The example below prints 0 even though the loop runs twice:
count=0
printf 'a\nb\n' | while read -r line; do count=$((count + 1)); done
echo "$count"The fix is to feed the loop from a redirection rather than a pipe, as in done < file, or to use process substitution, which keeps the loop in the current shell. Recognizing this trap saves a long debugging session, because the loop clearly works, and the variable looks as if nothing happened.
Hands-on: read a name and lines
Create name.sh with a single read and a greeting, and try it with a piped name and with no input. Then test the last-line rule by feeding a value with no newline at the end. Use printf to build the input, since printf does not add a newline unless you ask for one.
printf 'no newline at end' | while IFS= read -r line || [ -n "$line" ]; do echo "[$line]"; doneThe loop prints the last line in brackets. If it prints nothing, the test after the || is missing, and the final line was lost.