Back to the command reference

read

ShellFundamentals In simulator In real Linux

Read a line of input into variables

Name: read

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

read is a shell builtin that reads a line from standard input, splits it on whitespace and assigns the pieces to the named variables, the last one getting the rest.

The idiom while IFS= read -r line processes a file line by line; -p shows a prompt, -s hides input (passwords) and -t sets a timeout.

Syntax

read [OPTION]... [NAME]...

Common options

-rDon't treat backslashes as escapes
-p PROMPTShow a prompt first
-sDon't echo input (for passwords)
-t TIMEOUTWait at most TIMEOUT seconds; fail on timeout
-n NCHARSReturn after NCHARS characters, without Enter
-a ARRAYStore the words in the array ARRAY
-d DELIMRead up to the character DELIM (instead of a newline)

Examples

  • $ read -p "Your name: " name

    Ask the user for input

    Explain
  • $ while read line; do echo "$line"; done < file.txt

    Process a file line by line

    Explain

Related