As most of you know, we can define variables in our shell scripts to make our scripts more independent of hard coding. Below are they ways we can assign values to variables in a shell script

Defining variables in the script: Assigning predefined constants to variables with the script.
Before executing a script: Using positional parameters we can assign values to variables before actual execution of the script
When running a script: We can assign values to variables when we are in middle of the script.

All these solve different purposes on when to assign values to variables. The read command is useful for assigning variables at the time of executing a script, a kind of interactive script.

Learn read command with examples

The read command syntax


To access the above variable we use “$” or use echo if you want to print it.

echo "My variable is $VARIABLE_NAME"

Some frequently used read command examples

Example1: Read a value from user input. To display this value, we have to use echo command as mention earlier.

read VAR1
echo $VAR1


[email protected]:~$ read VAR1
[email protected]:~$ echo $VAR1

Example2: The read command is an excellent command which can read two or more words/variable/values at a time.

read VAR1 VAR2


[email protected]:~$ read VAR1 VAR2
surendra kumar
[email protected]:~$ echo $VAR1
[email protected]:~$ echo $VAR2

Example3: We can use read command to read elements of an array. Make sure that you separate each item with space. As by default arrays uses spaces to separate items in the shell.


To display first value in array use below command

echo ${ARR1[0]}


[email protected]:~$ read ELEMENTS1
surendra kumar anne
[email protected]:~$ ARR1=($ELEMENTS1)
[email protected]:~$ echo ${ARR1[0]}
[email protected]:~$ echo ${ARR1[1]}
[email protected]:~$ echo ${ARR1[2]}

Example4: We can read multiple values from a command

read VAR1 VAR2 VAR3 << ( echo surendra kumar anne )
echo "Enter values are $VAR1 $VAR2 $VAR3"

Example 5: Till this point, we saw just entering some data for a given variable. But if we can provide some meaningful data for the user when entering data that will be great. This can be achieved by using echo and read commands

Related concept:   How To Set Java Path In Linux?


echo "Please enter your name"
read NAME1


[email protected]:~$ echo "Please enter your name: "
Please enter your name:
[email protected]:~$ read NAME1
[email protected]:~$ echo $NAME1

Example 6: This is not that meaningful right? We can club echo and read command from above example with -p option of reading. This option prints some useful message for the user.

[email protected]:~$ read -p "Please enter your name: " NAME1
Please enter your name: Surendra
[email protected]:~$ echo "My name is $NAME1"
My name is Surendra

Other examples which we have seen until this point with -p option


[email protected]:~$ read -p "Please enter your name: " NAME1
Please enter your name: Surendra
[email protected]:~$ echo "My name is $NAME1"
My name is Surendra

[email protected]:~$ read -p "Give your first and last names: " FNAME1 LNAME1
Give your first and last names: Surendra Anne
[email protected]:~$ echo "My first name is $FNAME1, and my last name is $LNAME1"
My first name is Surendra, and my last name is Anne

[email protected]:~$ read -p "Give your first, middle and the last names: " NAME1
Give your first, middle and the last names: Surendra Kumar Anne
[email protected]:~$ ANAME1=($NAME1)
[email protected]:~$ echo "My first name is ${ANAME1[0]}, middle name is ${ANAME1[1]}, and last name is ${ANAME1[2]}"
My first name is Surendra, middle name is Kumar, and last name is Anne

Example7: The read command has an inbuilt variable called REPLY. This is system variable which stores value into $REPLY.

read -p "Please enter a value."
echo "Enter value is $REPLY"

Example8: How can I provide some value without showing at the terminal? Use -s which suppress the echo output.

read -ps "Please enter the password"


Please enter the password

Example 9: Set a timeout for your read command so that it will not wait indefinitely. We can use system variable called TMOUT to set this timeout for your shell script.

[email protected]:~/sh# cat
read -p "Enter your name:"
if [ -z $REPLY ]
echo -en "\nTaking the default name as Linux\n"
echo -en "Welcome $REPLY\n"
[email protected]:~/sh# time bash
Enter your name:surendra
Welcome surendra
real 0m2.227s
user 0m0.000s
sys 0m0.000s
[email protected]:~/sh# time bash
Enter your name:
Taking the default name as Linux
real 0m5.002s
user 0m0.000s
sys 0m0.000s

If you observe when we don’t enter anything the read command waits for 5seconds and then continue with other executions.

Related concept:   How to install CPAN package and configure it in Linux?