read - Linux


The read command in Linux is used for reading a line of input from standard input. The primary purpose of this command is to accept user input or pipe another command’s output into a script. It is most effectively used in shell scripting where interactive user input is needed or when you want to process data line by line.


The basic syntax of the read command is as follows:

read [options] [variables...]
  • [variables…] are the names of the variables that will store the inputs from the standard input. If no variables are specified, the input goes to the built-in variable REPLY.


  • -p: Prompt string. Outputs the string immediately before the read begins.
  • -s: Silent mode. Do not echo input coming from a terminal.
  • -r: Do not allow backslashes to escape any characters.
  • -t TIMEOUT: Timeout in seconds. If input is not provided within the specified seconds, the read will return a non-zero exit status.
  • -n NCHARS: Return after reading NCHARS characters rather than waiting for a complete line of input.
  • -d DELIM: Continue until the first character of DELIM is read, rather than newline.
  • -a ARRAY: Read from the standard input into an indexed array variable ARRAY, starting at index 0.


  1. Basic Input Read:

    read name
    echo "Hello, $name!"
  2. Silent Input for Password:

    read -s -p "Enter Password: " pass
    echo -e "\nPassword is set."
  3. Timeout for Input:

    read -t 10 -p "Enter data within 10 seconds: " data
    echo "You entered: $data"
  4. Read Multiple Values:

    echo "Enter three numbers:"
    read num1 num2 num3
    echo "You entered: $num1, $num2, $num3"
  5. Read Array:

    echo "Enter names separated by space:"
    read -a names
    echo "The names are: ${names[0]}, ${names[1]}"

Common Issues

  1. Silent Mode Pitfalls: When using -s, users won’t see their input, which can sometimes lead to errors in typing, especially in passwords.

  2. Missing Characters: When using -n, make sure to account for the termination character counts as a character.

  3. Timeout Too Short: Setting a very short timeout with -t may not give users enough time to enter the required information.


read can be integrated with other commands to create powerful scripts, e.g., reading user input to interactively find text within a file:

echo "Enter the search word:"
read word
grep "$word" sample.txt
  • echo: Used to display a line of text/string that is passed as an argument.
  • printf: Similar to echo but allows more formatted output.
  • grep: Used to search through text to match regex patterns.

For further reading and in-depth knowledge, consult the bash man page (man bash) or online Bash scripting resources.