Bash read command

Read or die friends. The read command is just as important as positional parameters and the echo command. How else are you going to catch user input, accept passwords, write functions, loop, and peek into file descriptors? Read on.

What is read?

Read is a bash builtin command that reads the contents of a line into a variable. It allows for word splitting that is tied to the special shell variable IFS. It is primarily used for catching user input but can be used to implement functions taking input from standard input.

Bash read builtin command help

Before we dive into how to use the read command in bash scripts, here is how we get help. There you should see all the options available for the read command along with descriptions that we will try to cover in the examples.

Reads a single line from the standard input, or from file descriptor FDif the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in$IFS are recognized as word
delimiters.

If no NAMEs are supplied, the line read is stored in the REPLY variable.

Options:-a array assign the words read to sequential indices of the array
variable ARRAY, starting at zero-d delim continueuntil the first character of DELIM is read, rather
than newline
-e use Readline to obtain the line in an interactive shell-i text use TEXT as the initial text for Readline-n nchars return after reading NCHARS characters rather than waitingfor a newline, but honor a delimiter if fewer than

NCHARS characters are read before the delimiter-N nchars return only after reading exactly NCHARS characters, unless
EOF is encountered or readtimes out, ignoring any
delimiter-p prompt output the string PROMPT without a trailing newline before
attempting to read
-r do not allow backslashes to escape any characters
-s do not echo input coming from a terminal-t timeout time out and return failure if a complete line of
input is not read within TIMEOUT seconds. The value of the
TMOUT variable is the default timeout. TIMEOUT may be a
fractional number. If TIMEOUT is 0, read returns
immediately, without trying to read any data, returning
success only if input is available on the specifiedfile descriptor. The exit status is greater than 128if the timeout is exceeded-u fd read from file descriptor FD instead of the standard input

Prompt user

In interactive bash scripts prompting a user may require a message to tell the user what input is expected. We can always accomplish this using the echo builtin. However, it turns out there is an option using read.

Prompt user for a word

In catching a word of input, we used echo to write Type something and hit space: to standard output before read ‘-d ‘. The -p option allows a message to be displayed before reading from standard input.

Prompt user for a secret

When catching user input without it showing up int the terminal, the -s option comes in handy. read -s -p allows you to catch and hide user input as follows.

Commands

{read-s-p'Type something I promise to keep it a secret: 'echo"";echo"Your secret is safe with me" ; unset REPLY ;echo"${REPLY}"}

Output

Type something I promise to keep it a secret:
Your secret is safe with me

Functions using read

Here are examples of functions in bash that use read and standard input

Core concept

Functions using read make use of piped standard input and parameters. Main input to be process such as lines in a file are passed in through standard input via a pipe. Other input if-any and option are passed in as parameters.

read-t1 NAME1 NAME2 ...

read is a builtincommand

-t 1 prevent the bash script from waiting indefinitely for a line to be returned through standard input. If standard input is initially empty, the function returns with an exit code of 142 signifying that no date was read within the set timeout period

NAME1 NAME2 are variable names

... many variable names may be listed

Now that the groundworks are set, let’s see what familiar functions look like implemented using read.

Join function using read

Suppose we want a join function that takes a list of words and returns another list of words joined by a delimiter. Here is how we may implement a join function using read.

Map functions using read

Suppose we want a map function that takes a list and returns another list containing the same number of elements that are modified by another function. Here is how we may implement a map function using read.

Loops using read

Loops using read allow you to iterate through lines of a file that is to be generated or already exists.

Basic while read loop for the lefthand side (lhs)

We have a command or function (lhs) that can generate lines in a file that can be looped through using read and a while loop.

Construct

lhs |whilereaddotruedone
lhs is a command that returns a list of lines

Commands

seq5|whileread idoecho${i}done

Output

12345

Basic while read loop for the righthand side (rhs)

We have a file (rhs) with lines that can be looped through using read and a while loop.

Construct

whilereaddotruedone< rhs

rhs is a file containing lines

Commands

seq5> rhswhileread idoecho${i}done< rhs

Output

12345

Custom lhs while loop using read

We have a stream of words that we would like to loop through using read.

Construct

(IFS=" "
lhs |whilereaddotruedone)

lhs is a list of words

Commands

(IFS=" "echo{1..5}|whileread idoecho"${i}
done
)

Output

12345

Reading from any fd instead of standard input

The read builtin option often left untouched is the one that allows you to specific what file descriptor to read from, read -u FD. By default FD is taken to be standard input.

Core concept

When a file opened file descriptors are assigned. IO redirection in bash allow a file to be left open with a specific file descriptor. We are allowed to write to the file, read from it, and close it when we are done.

Building a train with file descriptors and read -u FD

Just for fun I decided to build a train with file descriptors and read -u FD. To each file descriptor a number is written. Each file descriptor reads from the file descriptor 1 below and appends to itself.

Skip function using read -u FD

it may be possible due to a bug to implement a skip function that skips the following line in a bash script outside of functions before the script source is read. Note that it does not work on most systems. For example,

Bottom line

The read builtin in bash does more than catch user input. It can be used in functions, loops and exchanges between file descriptors used in bash scripts. On occasion exploration using read and file descriptors may yield Easter eggs.

About the author

Nicholas Shellabarger

A developer and advocate of shell scripting and vim. His works include automation tools, static site generators, and web crawlers written in bash. For work he tools with cloud computing, app development, and chatbots. He codes in bash, python, or php, but is open to offers.