The read builtin

read [-ers] [-u fd] [-t timeout] [-p <PROMPT>] [-a <ARRAY>] [-n <NCHARS>] [-d <DELIM>] [<NAME...>]

The read builtin reads one line of data (text, user input, …) from standard input or a supplied filedescriptor number into one or more variables named by <NAME…>.

If <NAME…> is given, the line is word-split using IFS variable, and every word is assigned to one <NAME>. The remaining words are all assigned to the last <NAME> if more words than variable names are present.

If no <NAME> is given, the whole line read (without performing word-splitting!) is assigned to the shell variable REPLY. Then, REPLY really contains the line as it was read, without mangeling pre- and postfix spaces and other things!

read can do much more for you, here's a option list:

OptionDescription
-a <ARRAY>read the data word-wise into the specified array <ARRAY> instead of normal variables
-d <DELIM>recognize <DELIM> as data-end, rather than <newline>
-eon interactive shells: use Bash's readline interface to read the data
-n <NCHARS>reads <NCHARS> characters of input, then quits
-p <PROMPT>the prompt string <PROMPT> is output (without a trailing automatic newline) before the read is performed
-rraw input - disables interpretion of backslash escapes and line-continuation in the read data
-ssecure input - don't echo input if on a terminal (passwords!)
-t <TIMEOUT>wait for data <TIMEOUT> seconds, then quit (exit code 1)
-u <FD>use the filedescriptor number <FD> rather than stdin (0)

When both, -a <ARRAY> and a variable name <NAME> is given, then the array is set, but not the variable.

Code examples

Rudimentary cat replacement

A rudimentary replacement for the cat command: read lines of input from a file and print them on the terminal.

opossum() {
  while read -r; do
    printf "%s\n" "$REPLY"
  done <"$1"
}

Note: Here, read -r and the default REPLY is used, because we want to have the real literal line, without any mangeling. printf is used, because (depending on settings), echo may interpret some baskslash-escapes or switches (like -n).

Press any key...

Remember the MSDOS pause command? Here's something similar:

pause() {
  local dummy
  read -s -p "Press any key to continue..." -n 1 dummy
}

Reading Columns

Simple Split

Read can be used to split a string:

var="one two three"
read -r col1 col2 col3 <<< "$var"
printf "col1: %s col2: %s col3 %s\n" "$col1" "$col2" "$col3"

Take care that you cannot use a pipe:

echo "$var" | read col1 col2 col3 # does not work!
printf "col1: %s col2: %s col3 %s\n" "$col1" "$col2" "$col3"

Why? because the commands of the pipe run in subshells that cannot modify the parent shell. As a result, the variables col1, col2 and col3 of the parent shell are not modified (see article: Bash and the process tree).

If the variable has more fields than there are variables, the last variable get the remaining of the line:

read col1 col2 col3 <<< "one two three four"
printf "%s\n" "$col3" #prints three four

Changing The Separator

By default reads separates the line in fields using spaces or tabs. You can modify this using the special variable IFS, the Internal Field Separator.

IFS=":" read -r col1 col2 <<< "hello:world"
printf "col1: %s col2: %s\n" "$col1" "$col2"

Here we use the var=value command syntax to set the environment of read temporarily. We could have set IFS normally, but then we would have to take care to save its value and restore it afterward (OLD=$IFS IFS=”:”; read ….;IFS=$OLD).

The default IFS is special in that 2 fields can be separated by one or more spaces. When you set IFS to something else, the fields are separated by exactly one character:

IFS=":" read -r col1 col2 col3 <<< "hello::world"
printf "col1: %s col2: %s col3 %s\n" "$col1" "$col2" "$col3"

See how the :: in the middle infact defines an additional empty field.

The fields are separated by exactly one character, but the character can be different between each field:

IFS=":|@" read -r col1 col2 col3 col4 <<< "hello:world|in@bash"
printf "col1: %s col2: %s col3 %s col4 %s\n" "$col1" "$col2" "$col3" "$col4"

Are you sure?

asksure() {
echo -n "Are you sure (Y/N)? "
while read -r -n 1 -s answer; do
  if [[ $answer = [YyNn] ]]; then
    [[ $answer = [Yy] ]] && retval=0
    [[ $answer = [Nn] ]] && retval=1
    break
  fi
done

echo # just a final linefeed, optics...

return $retval
}

### using it
if asksure; then
  echo "Okay, performing rm -rf / then, master...."
else
  echo "Pfff..."
fi

See also

commands/builtin/read.txt (446 views) · Last modified: 2008/06/18 06:50 by thebonsai
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0