Theread
commandisa built-in shell command for reading data from a file or keyboard.
Theread
command receives the input from the keyboard or a file until it receives a newline character. Then, it converts the newline character into a null character:
- Read a value and store it in the variable, shown as follows:
read variable
echo $variable
This will receive text from the keyboard. The received text will be stored in the variable.
- Whenever we need to display the prompt with certain text, we use the
-p
option. The option-p
displays the text that is placed after-p
on the screen:
#!/bin/bash
# following line will print "Enter value: " and then read data
# The received text will be stored in variable value
read -p "Enter value : " value
This is the output:
Output:Enter value : abcd
- If the variable name is not supplied next to the
read
command, then the received data or text will be stored in a special built-in variable calledREPLY
. Let’s write a simpleread_01.sh
script, shown as follows:
#!/bin/bash
echo "Where do you stay ?"
read # we have not supplied any option or variable
echo "You stay in $REPLY
Save the file, give the permission to execute, and run the script as follows:
$ chmod u+x read_01.sh$
This is the output:
Output:"Where do you stay?"Mumbai"You stay at Mumbai"
- We will write the script
read_02.sh
. This script prompts the user to enter their first and last name to greet the user with their full name:
#!/bin/bash
echo "Enter first Name"
read FIRSTNAME
echo "Enter Last Name"
read LASTNAME
NAME="$FIRSTNAME $LASTNAME"
echo "Name is $NAME"
- For reading text and storing in multiple variables, the syntax is as follows:
$ read value1 value2 value3
Let’s write the shell script, read_03.sh
, shown as follows:
#!/bin/bash
echo "What is your name?"
read fname mname lname
echo "Your first name is : $fname"
echo "Your middle name is : $mname"
echo "Your last name is : $lname"
Save the file, give the permission to execute, and run the script as follows:
Output:What is your name?
Ganesh Sanjiv Naik
"Your first name is : Ganesh"
"Your middle name is : Sanjiv"
"Your last name is : Naik"
- Let’s learn about reading a list of words and storing them in an array:
#!/bin/bash
echo -n "Name few cities? "
read -a cities
echo "Name of city is ${cities[2]}."
Save the file, give the permission to execute, and run the script as follows:
Output:Name few cities? Delhi London Washington Tokyo
Name of city is Washington.
In this case, the list of cities is stored in the array of cities. The elements in the array are here:
cities[0] = Delhicities[1] = Londoncities[2] = Washingtoncities[3] = Tokyo
The index of the array starts with 0
, and, in this case, it ends at 3
. In this case, four elements are added to the cities[]
array.
- If we want the user to press theEnterkey, then we can use the
read
command along with one unused variable, shown as follows:
Echo "Please press enter to proceed further "read tempecho "Now backup operation will be started ! "
Summarizing the read command with options
The following table summarizes various read command-related options that you learned in the previous sections:
Format
Meaning
read
This command will read text from a keyboard and store the received text in a built-in variableREPLY
.
read value
This reads text from a keyboard or standard input and stores it into the variable value.
read first last
This will read the first word in a variable first and the remaining text of the line in a variable last. The first word is separated by white space from the remaining words in the line.
read -e
This is used in interactive shells for command-line editing. If vi editor is used, then vi commands can be used.
read -a array_name
This will store a list of words received in an array.
read -r line
Text with a backslash can be received here.
read -p prompt
This will print the prompt and wait for the user input. The received text will be stored in the variableREPLY
.