Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 95 ยท Shell Scripting

Reading Input and Interactive Scripts

Master the Bash read builtin: -r, -p, -s, -n, -t, -a, IFS splitting, reading until EOF, validating input, default answers, confirm prompts and [[ -t 0 ]] checks.

So far our scripts got their data from arguments. Many scripts also need to read input: a name typed by the user, a file piped in, a list of values separated by commas. In Bash the workhorse is the builtin read. It reads one line from standard input, splits it into words, and stores them in variables.

In this lesson you will learn how read splits lines with IFS, why you should almost always write read -r, what the options -p, -s, -n, -t and -a do, how to read a whole stream until end of file, how to validate answers and offer defaults, how to ask yes/no questions, and how to tell whether your script is running interactively. It follows Here-Documents, Here-Strings and Input and Standard Streams and Redirection.

In our editor. The sandbox has no keyboard for your script. Every example below that reads input has its text preset in the "Program input" box, which is delivered to the script's standard input. In a real terminal, read waits for you to type a line and press Enter.

The basics of read

read name reads one line from stdin and stores it in name. With several variable names, the line is split into words: each name gets one word, and the last name gets everything that is left.

Bash read stores a line in one or more variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first line has two words for two variables. The second line has four words; first takes one and rest takes the remaining three, spaces included.

If you give no variable name, the line goes into the default variable REPLY. The exit status of read is 0 when it read a line and non-zero at end of input, which is what makes while read ... loops work.

Always use -r

Without -r, read treats a backslash as an escape character: it removes it and joins lines that end in a backslash. That is almost never what you want, especially for file paths and regular expressions.

Bash read with and without -r

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Without -r the backslashes in the path were swallowed (and a backslash at the end of a line glued the next line on); with -r the text arrives exactly as typed. Rule of thumb: read -r unless you have a specific reason. ShellCheck warns about a missing -r for the same reason (SC2162).

Splitting with IFS

IFS (the Internal Field Separator) lists the characters read uses to split a line. Its default is space, tab and newline. To split on something else, set IFS only for the read command by putting the assignment before it:

Terminal
IFS=, read -r a b c      # the assignment applies to this one read
Bash Splitting on commas, colons and tabs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Three details worth remembering:

  • _ is a common name for "a field I do not care about".
  • $'\t' is Bash's way to write a literal tab (Quoting).
  • When IFS is whitespace (the default), runs of spaces count as one separator and leading and trailing whitespace is stripped. When it is a non-whitespace character like a comma, a,,c has an empty second field.

Keeping leading spaces: IFS=

If you want the line exactly as it is, including leading and trailing spaces, set IFS to nothing: IFS= read -r line. This is the standard idiom for reading a line without any modification.

Bash IFS= keeps the whole line intact

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Reading until end of file

To process every line of input, loop on read. When there is no more input read returns non-zero and the loop ends. The correct idiom has two parts you have just learned: IFS= and -r.

Bash The while IFS= read -r loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The empty line counts as a line, and the backslash line arrived unchanged. See also Loops.

The last line without a newline. If the final line of input does not end with a newline, read still stores its text but returns non-zero, so the loop body is skipped for that line. The fix is the extra test || [[ -n $line ]]:

Bash A last line with no trailing newline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The input can come from a file with <, from a pipe, or from a here-string, but be careful with pipes: cmd | while read ... runs the loop in a subshell, so variables set inside it are lost afterwards. Feed the loop with < <(cmd) (process substitution) or a temporary file when you need the values later (Subshells).

Reading a delimited record: CSV-style lines

Combine IFS with several variables to parse simple delimited data, one record per line.

Bash Summing a simple CSV file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The braces { ...; } group the two reads so that they share one redirection: the first read takes the header line and the loop continues with the same open file. Without the group, each read would reopen the file and start from line 1. Bash cannot do decimal arithmetic itself, so awk is used for the money (Arithmetic). Real CSV with quoted commas needs a real parser; read only handles the simple kind.

Options of read

Option What it does
-r Do not interpret backslashes (use it nearly always)
-p "text" Print a prompt, without newline, only when stdin is a terminal
-s Silent: do not echo what is typed (passwords)
-n N Return after N characters (no Enter needed)
-N N Like -n, but ignore delimiters and read exactly N characters
-t SECONDS Give up after a timeout; status is greater than 128 on timeout
-d CHAR Stop at CHAR instead of newline (-d '' reads up to a NUL byte)
-a NAME Store the words in the indexed array NAME
-u FD Read from file descriptor FD instead of stdin

The options -p and -s matter only for a person at a keyboard. When the input is not a terminal (a pipe, a file, our Input box) they do nothing visible: -p prints no prompt (so use printf if the prompt must always appear) and -s has no echo to hide.

Bash -a, -n and -d

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Look at the last block: -d '' makes read stop at a NUL byte instead of a newline, which here means "read the entire here-string". Combined with IFS=';' and -a, it splits a delimited string into an array.

Timeouts

read -t 5 waits at most five seconds. On timeout it returns a status above 128 and leaves the variable empty. That is how scripts offer a default after some time without an answer.

Bash read -t gives up (stdin is empty)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

When the input ends (EOF, as with /dev/null) the status is 1; when the time runs out it is 142 (128 + SIGALRM). Both are "failure", but you can tell them apart if it matters.

Validating input and default answers

A user (or a data file) can always give you something unexpected: an empty line, text where you need a number, a value out of range. Loop until the value is valid, or give up with a clear error.

Defaults are easy with parameter expansion: ${answer:-default} uses the default when answer is empty (Parameter Expansion).

Bash Asking until the answer is valid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The preset input is: an empty line (the first call takes the default 1), then abc (rejected), -5 (out of range) and 12 (accepted) for the second call. The third call finds no input left. Because standard error appears after standard output in our output panel, the rejection messages are shown after the prompts; in a real terminal they appear in order. The function handles that running-out of input (EOF) by falling back to the default, which prevents an infinite loop when a script is run with input that ends early. Always think about what your loop does at EOF.

Confirm prompts

A yes/no question is the most common prompt. Normalise the answer with lower-casing (${reply,,}) and a case. The default is usually the safe choice, shown in capitals as in [y/N].

Bash A reusable confirm function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Anything that is not a clear yes counts as no, including the empty line and maybe. For destructive actions that is the right design. case is covered in case Statements; select can build numbered menus but needs a human, so it is read-only in our editor.

Passwords and single keys (read-only)

Two interactive features cannot be shown in the sandbox, because they need a real terminal:

Terminal
# Run this in a real terminal, not in our editor.
read -r -s -p "Password: " password   # -s hides what you type
echo                                   # the Enter you pressed was not echoed

read -r -n 1 -p "Press any key to continue... "   # no Enter needed
echo

Never pass passwords as command-line arguments (they show up in ps), and avoid putting them in environment variables you do not need. Reading with -s into a variable, using it, and unsetting it is better.

Interactive or not? [[ -t 0 ]]

A good script behaves differently depending on whether a person or a program is on the other end. The test [[ -t 0 ]] is true when file descriptor 0 (stdin) is attached to a terminal; -t 1 checks stdout. Use it to decide whether to ask questions or to use defaults, or to print colours only on a terminal.

Bash Detecting whether stdin and stdout are terminals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

In the sandbox both tests are false, so the script reads from the preset input. This is the same decision you will make for scripts that run from cron or a CI system, where nobody can answer a question: provide an option such as --yes or --force for those cases (Script Arguments and Options), and fail with an error instead of waiting forever when input is needed but unavailable.

Common mistakes

  • Forgetting -r: backslashes disappear.
  • Using echo $line after reading: unquoted, it collapses spaces. Use "$line".
  • cmd | while read then using the variables later: the loop ran in a subshell. Use < <(cmd).
  • Reading a file inside a loop that also reads stdin: the inner read eats the file's lines. Use another file descriptor: while read -r -u 3 line; do ...; done 3< file.
  • Loops that never end when input is exhausted: check read's status.
  • Trusting input: never pass raw user input to eval or build commands from it; see Quoting.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does read -r first rest do with the line one two three?

    Each variable except the last gets one word. The last variable receives the rest of the line, including the spaces between the remaining words.
  2. 2. Which loop correctly reads every line of a file, keeping leading spaces and backslashes?

    IFS= prevents trimming of leading and trailing whitespace and -r prevents backslash processing. The for loop over $(cat file) splits on every whitespace, not on lines.
  3. 3. A script is run as ./script.sh < answers.txt. What does [[ -t 0 ]] return inside it?

    -t 0 is true only when file descriptor 0 is connected to a terminal. Redirecting a file onto stdin makes it false, which is how scripts detect non-interactive use.
  4. 4. What is $REPLY for?

    When read is called without a variable name, the whole line is stored in REPLY.
  5. 5. How do you split the string a:b:c into the array parts using read?

    Setting IFS to a colon for that one command makes read split on colons, and -a stores the words in an array. The assignment before the command applies only to that command.

Summary

  • read -r var reads one line from stdin; several names split it into words, the last name gets the rest; with no name, the line goes into REPLY.
  • Use read -r always and IFS= read -r line to keep a line exactly as it is; set IFS for a single read to split on commas, colons or tabs.
  • while IFS= read -r line; do ...; done < file reads every line; add || [[ -n $line ]] for a missing final newline.
  • -p, -s, -n, -t, -a and -d add prompts, hidden input, single characters, timeouts, arrays and custom delimiters; prompts and silence only matter on a terminal.
  • Validate input in a loop, offer defaults with ${reply:-default}, treat anything but a clear yes as no, and handle EOF.
  • Use [[ -t 0 ]] to tell interactive use from pipes and cron, and give scripts non-interactive options.

Next: Debugging Shell Scripts