Session and utilities

The read command

Reads one line of input and stores it in a variable.

Syntax

read [-p mensaje] variableTry it in the terminal →

Most useful options

-p mensajeShows a prompt before reading. It only appears when the input comes from a terminal.
-sDoes not echo what you type, useful for passwords. It only has effect on a terminal.
-rBackslash stops escaping the next character and is kept as it is.
-t segundosWaits at most that long. If no line arrives, it returns an exit status above 128.
-n caracteresStops after that many characters without waiting for Enter, unless the delimiter comes first.
-a arrayStores each word in an indexed array, starting at 0.
-d delimUses the first character of delim as the line end instead of the newline.

Copy-ready examples

$ read NOMBREwaits for a line and stores it in NOMBRE. It prints nothing.
$ printf 'Ana Pérez\n' | { read nombre apellido; echo "$apellido, $nombre"; }reads two words from a pipe and splits them between two variables.

Example output

Pérez, Ana
$ printf 'a b c\n' | { read x y; echo "[$x][$y]"; }the last variable receives the rest of the line, spaces included.

Example output

[a][b c]
$ read -r -p "Ruta: " rutaasks for a path in a terminal without interpreting backslashes.
$ while IFS= read -r linea; do echo "> $linea"; done < notas.txtgoes through a file line by line without losing spaces or backslashes.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
bash: read: -q: invalid optionread has no -q option. Right after, bash prints a usage line with the valid options.Check the options listed above and use only the ones that exist.
bash: read: `1x': not a valid identifierThe variable name starts with a number or contains characters that are not valid.Use names that start with a letter or an underscore, such as cantidad1 or _x.
bash: X: readonly variableThe variable is read-only, created with readonly, and read cannot change its value.Use another name. To see which variables are read-only, run readonly -p.

Typical mistakes

  • In a pipe, read runs in a subshell: the variable does not reach your session. Use a < redirection instead.
  • Without -r, the backslash is removed and the next character is taken literally. With -r the backslash is kept.
  • If the line has more words than names, the rest goes to the last name with its spaces. With read x y and the input 'a b c', y is 'b c'.

Differences from similar commands

  • readarray: reads all the input lines into an array at once. read reads one line per call.
  • sh: POSIX only guarantees -r and, since its 2024 edition, -d. A script meant for /bin/sh should not depend on -s, -a, -n or -t, which are extensions.

Related commands

FAQ

How do I ask for a password without showing what I type?

With read -s. It only hides what you type when the input comes from a terminal; from a pipe there is nothing to hide.

What does read store if I give it no name?

The whole line, without the final newline, in the variable REPLY.

How do I read a file line by line?

With while IFS= read -r line; do ...; done < file. An empty IFS keeps the spaces at the edges.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course