Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

read

Read one line of input (from the keyboard, a file or a pipe) into variables.

Level
RHCSA exam
Group
Shell and help
Needs root
No
Package
bash (built-in)

What it does

read NAME waits for a line and stores it in NAME. With more names, the words are split: the first word to the first name, the rest to the last one. With no name the line goes to $REPLY. while read -r LINE; do …; done < FILE is the standard way to process a file line by line.

How to write it

read [-ers] [-a ARRAY] [-d DELIM] [-i TEXT] [-n N] [-N N] [-p PROMPT] [-t TIMEOUT] [-u FD] [NAME]...

Options 11

Most used

-p PROMPT
Show a prompt first.
-r
Backslashes are normal characters (use it almost always).
-s
Silent: do not show the typed text (passwords).
-t SECONDS
Give up after this time.
-n N
Return after N characters.
-N N
Read exactly N characters.
-a ARRAY
Store the words in an array.
-d DELIM
Read until this character instead of a newline.
-e
Use readline (editing keys) in a terminal.
-i TEXT
Default text for -e.
-u FD
Read from this file descriptor.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Ask a question

    read -p "Your name: " NAME; echo "Hello $NAME"

    Simple input in scripts.

  2. 2Ask for a password

    read -s -p "Password: " PASS; echo

    Nothing is shown while typing.

  3. 3Process a file line by line

    while read -r LINE; do echo "line: $LINE"; done < /etc/hostname

    -r keeps backslashes.

  4. 4Split fields

    echo "anna sales 3000" | { read -r NAME GROUP GID; echo "$NAME is in $GROUP ($GID)"; }

    Words go to the names in order.

  5. 5Split /etc/passwd fields

    while IFS=: read -r USER _ UID _; do [ "$UID" -ge 1000 ] && echo "$USER"; done < /etc/passwd

    IFS=: splits on colons.

  6. 6Yes/no with a timeout

    read -t 10 -n 1 -p "Continue? [y/N] " ANSWER; echo; [ "$ANSWER" = y ] || exit 1

    Defaults to no after 10 seconds.

  7. 7Into an array

    read -r -a PARTS <<< "a b c"; echo "${PARTS[1]}"
    b

    Index starts at 0.

  8. 8Read command output (not a pipe into while)

    while read -r PID CMD; do echo "$PID $CMD"; done < <(ps -eo pid=,comm= | head -3)

    Process substitution keeps variables after the loop.

  9. 9Default value with editing

    read -e -i "/backup" -p "Target folder: " DIR

    The user can edit the default.

  10. 10Pause a script

    read -r -p "Press Enter to continue"

    Waits for Enter.

Watch out

  • Without -r, backslashes in the input disappear.
  • cmd | while read … runs the loop in a subshell: variables set inside are gone afterwards.

Exit codes

0 :: A line was read. 1 :: End of file (or timeout > 128 with -t).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man read