Read Command

Bash Read Command Examples

Receiving input from the read command


The read command is a built in function that allows scripts to catch information entered by users interactively. The basic syntax of the "read" command is as follows:

read [options] VAR1 VAR2 .... VARN



The read command is used to get a line of input into a variable. Each argument must be a variable name without the leading "$". The built in command reads a line of input and separates the line into individual words using the "IFS" inter field separator. (see IFS. By default the "IFS" is set to a space. Each word in the line is stored in a variable from left to right. The first word is stored in the first variable, the second word to the second variable and so on. If there are fewer variables than words, then all remaining words are then assigned to the last variable. If you have more variables than words defined, then any excess variables are set to null. If no variable names are supplied to the read line, then the read uses the default variable REPLY.

read example



#!/bin/bash
#
read first middle last
echo "Hello $first $middle $last"

Output from above read example



john@john-desktop:~/scripts$ ./read1.sh 
land of linux
Hello land of linux

In the above "read" example, we passed the values "land", "of" and "linux" into the variables "first", "middle" and "last". These are then displayed via the echo command.

Although our script allowed us to enter our information, it could not't be classed as being very user friendly. To remedy this we can modify our script to use a simple echo statement:

read example



#!/bin/bash
#
echo "Please enter 3 words followed by ENTER:"
read first middle last
echo "Hello $first $middle $last"

Output from above read example



john@john-desktop:~/scripts$ ./read2.sh 
Please enter 3 words followed by ENTER:
land of linux
Hello land of linux

We could also use the "-p" flag with the read command:



read -p example


read -p example


#!/bin/bash
#
read -p "Please Enter 3 words followed by ENTER: " first middle last
echo "Hello $first $middle $last"

Output from above read -p example



john@john-desktop:~/scripts$ ./read3.sh 
Please Enter 3 words followed by ENTER: land of linux
Hello land of linux

In the above example we use the variables "first", "middle" and "last" after our prompt. We could always split the prompt across several lines as follows:

read -p example



#!/bin/bash
#
read -p "Please Enter first word followed by ENTER: " first 
read -p "Please Enter second word followed by ENTER: " middle 
read -p "Please Enter last word followed by ENTER: " last
echo "Hello $first $middle $last"

Output from above read -p example



john@john-desktop:~/scripts$ ./read4.sh 
Please Enter first word followed by ENTER: land
Please Enter second word followed by ENTER: of
Please Enter last word followed by ENTER: linux
Hello land of linux

read example - catch all


Sometimes there may be more words than defined variables. This is handled simply by all excess words being written to the last variable that we defined:

read -p example



#!/bin/bash
#
read -p "Please Enter some words followed by ENTER: " vara varb varc 
echo "vara contains $vara"
echo "varb contains $varb"
echo "varc contains any remaining words $varc"

Output from above read -p example



john@john-desktop:~/scripts$ ./read5.sh 
Please Enter some words followed by ENTER: one two three four five six
vara contains one
varb contains two
varc contains any remaining words three four five six

As we can see in the above example, the values "one" and "two" were assigned to variables "vara" and "varb". The remaining input was assigned to a single variable "varc". The variable "varc" contains the "three four five six".


Options available to the read command


Below is a table containing other parameters that may be used with the read built in in command:


Option Description
-a ANAME Words are assigned sequentially to the array variable ANAME
-d DELIM The first character of DELIM us used to terminate the input line
-e readline is used to get line
-n NCHARS read returns after reading NCHARS
-p PROMPT Display PROMPT without a trailing newline. Prompt is only displayed if coming from a terminal.
-r Backslash does not act as an escape character
-s Silent Mode. Characters are not echoed coming from a Terminal
-t TIMEOUT read will timeout after TIMEOUT seconds. Only from a Terminal
-u FD read input from Filed Descriptor FD