$_ CLI In Depth
Learning progress 0%
lesson scripting exit-codes control-flow

Reading Input with read

5 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH2 lines
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:

BASH3 lines
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:

BASH3 lines
while IFS= read -r line; do
  echo "got: $line"
done < input.txt

The 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:

BASH3 lines
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.

BASH1 lines
printf 'no newline at end' | while IFS= read -r line || [ -n "$line" ]; do echo "[$line]"; done

The loop prints the last line in brackets. If it prints nothing, the test after the || is missing, and the final line was lost.

Why a variable set in a pipeline disappears

Visualizer

Every part of a pipeline runs in its own process, so a while loop at the end of a pipe changes a copy of the variables. Here are three ways to keep the change.

1 / 4
echo xpart 1
while read l; do n=1; donepart 2

The | makes a pipeline. Bash starts each part of a pipeline in its own process, and the loop is one of those parts.

unset n; echo x | while read l; do n=1; done; echo "n=${n-unset}"
Terminal lab · 2 steps · real Linux in your browser

Read lines and input safely

Read one answer from standard input, then loop over lines and keep their spaces and backslashes.

01
Read a name from standard input
Create name.sh in /workspace. It reads one line from standard input with read and prints hello followed by the name. If standard input is empty, it prints no name to standard output and exits with status 1. Test it with a piped name and with an empty standard input.
Show hint for step 1
read returns a nonzero status when it reaches the end of input without reading anything. Put read in the condition of an if statement.
02
Print lines without changing them
Create lines.sh in /workspace. It reads standard input one line at a time and prints each line inside square brackets. It must keep spaces and backslashes exactly as they are, including leading spaces. It must also print a last line that has no trailing newline. Test it with a line that has two spaces inside it, a line with a backslash, and a final line with no newline.
Show hint for step 2
Set IFS to an empty value for read and use the -r option. Add the test [ -n ] after read, so a final partial line still runs the loop body.
Check yourself Question 1 of 4
Concept Check Purpose of the -r option recall

What does the -r option of the read builtin do?

Recap & Key Takeaways

read stores one line in named variables, splits it at IFS characters, and returns status 1 at end of input.
Use read -r with IFS empty in a while loop to keep each line unchanged, including spaces and backslashes.
A final line with no newline is still stored, so the loop needs the || [ -n "$line" ] test to process it.
A while loop at the end of a pipeline runs in a subshell, so feed it with a redirection when its variables must survive.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue