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:

[email protected]:~$ read VAR1
surendra
[email protected]:~$ 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:

[email protected]:~$ read VAR1 VAR2
surendra kumar
[email protected]:~$ echo $VAR1
surendra
[email protected]:~$ 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:

[email protected]:~$ read ELEMENTS1
surendra kumar anne
[email protected]:~$ ARR1=($ELEMENTS1)
[email protected]:~$ echo ${ARR1[0]}
surendra
[email protected]:~$ echo ${ARR1[1]}
kumar
[email protected]:~$ 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:

[email protected]:~$ echo "Please enter your name: "
Please enter your name:
[email protected]:~$ read NAME1
surendra
[email protected]:~$ 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.

[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

Output:

[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 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 with out showing at the terminal? Use -s which supress the echo output.

read -ps "Please enter the password"

Output:

Please enter the password