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

read VARIABLE_NAME

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

Output:

surendra@sanne-taggle:~$ read VAR1
surendra
surendra@sanne-taggle:~$ echo $VAR1
surendra

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

read VAR1 VAR2

Output:

surendra@sanne-taggle:~$ read VAR1 VAR2
surendra kumar
surendra@sanne-taggle:~$ echo $VAR1
surendra
surendra@sanne-taggle:~$ echo $VAR2
kumar

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.

read ELEMENTS1
ARR1=($ELEMENTS1)

To display first value in array use below command

echo ${ARR1[0]}

Output:

surendra@sanne-taggle:~$ read ELEMENTS1
surendra kumar anne
surendra@sanne-taggle:~$ ARR1=($ELEMENTS1)
surendra@sanne-taggle:~$ echo ${ARR1[0]}
surendra
surendra@sanne-taggle:~$ echo ${ARR1[1]}
kumar
surendra@sanne-taggle:~$ echo ${ARR1[2]}
anne

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

Example:

echo "Please enter your name"
read NAME1

Output:

surendra@sanne-taggle:~$ echo "Please enter your name: "
Please enter your name:
surendra@sanne-taggle:~$ read NAME1
surendra
surendra@sanne-taggle:~$ echo $NAME1
surendra

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.

surendra@sanne-taggle:~$ read -p "Please enter your name: " NAME1
Please enter your name: Surendra
surendra@sanne-taggle:~$ echo "My name is $NAME1"
My name is Surendra

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

Output:

surendra@sanne-taggle:~$ read -p "Please enter your name: " NAME1
Please enter your name: Surendra
surendra@sanne-taggle:~$ echo "My name is $NAME1"
My name is Surendra

surendra@sanne-taggle:~$ read -p "Give your first and last names: " FNAME1 LNAME1
Give your first and last names: Surendra Anne
surendra@sanne-taggle:~$ echo "My first name is $FNAME1, and my last name is $LNAME1"
My first name is Surendra, and my last name is Anne

surendra@sanne-taggle:~$ read -p "Give your first, middle and the last names: " NAME1
Give your first, middle and the last names: Surendra Kumar Anne
surendra@sanne-taggle:~$ ANAME1=($NAME1)
surendra@sanne-taggle:~$ 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"

Output:

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.

root@linuxnix:~/sh# cat lets_sleep.sh
#!/bin/bash
TMOUT=5
read -p "Enter your name:"
if [ -z $REPLY ]
then
echo -en "\nTaking the default name as Linux\n"
else
echo -en "Welcome $REPLY\n"
fi
root@linuxnix:~/sh# time bash lets_sleep.sh
Enter your name:surendra
Welcome surendra
real 0m2.227s
user 0m0.000s
sys 0m0.000s
root@linuxnix:~/sh# time bash lets_sleep.sh
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.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.