The read command is used to take an input from the user in the Bash script. If no variable is used after this command, the $REPLY variable is used to read the input value. The “read” command has many options to take an input which are explained in another tutorial. The use of the prompt option is one of them. This option is used to take an input from the user using a prompt message that helps the user to understand what type of data needs to be provided. The methods of using the prompt option to take an input from the user in Bash are explained in this tutorial.
Different Examples of Prompt Input
The different uses of the “read” command with the prompt message are shown in this part of the tutorial.
Example 1: Take the String Input Using Prompt
Create a Bash file with the following script that takes the first input without the prompt message into the default variable which is $REPLY, the second input without the prompt message into the $lang variable, and the third input with the prompt message into the $answer variable. Next, the messages are printed based on the input values. If the value of the $REPLY variable is “y”, “Y”, or “Yes”, the “You like programming” message is printed. If the value of the $lang variable is non-empty, the value of $lang is printed. If the value of the $answer variable is “y”, “Y”, or “Yes”, the “Your favorite language is” message is printed with the $answer value.
echo "Do you like programming? (y/n)"
#Read input in the default variable
read
echo "What is your favorite programming language?"
#Read input in the assigned variable
read lang
#Read input using a prompt message
read -p "Do you like Bash Programming?(Yes/No)" answer
echo
echo "***Output based on the input values:***"
echo
#Output based on the first input
if [[ $REPLY == "y" || $REPLY == "Y" || $REPLY == "Yes" ]]; then
echo "You like programming."
fi
#Output based on the second input
if [ $lang != "" ]; then
echo "Your favorite language is $lang"
fi
#Output based on the third input
if [[ $answer == "y" || $answer == "Y" || $answer == "Yes" ]]; then
echo "You like bash programming."
fi
The following output appears after executing the script with the first input value of “y”, the second input value of “PHP”, and the third input value of “y”:
Example 2: Take the Numeric Input Using Prompt
Create a Bash file with the following script that takes a numeric value from the user using a prompt message into the $number variable. Next, the value is compared with the particular numbers to find if the number matches with any ticket or not, and prints a message based on the return value of the “if” statement.
#Take a numeric input
read -p "Enter your ticket number [1000-9999]:" number
#Print output based on the input
if [[ $number -eq "7823" ]]; then
echo "You won the first prize."
elif [[ $number -eq "3489" ]]; then
echo "You won the second prize."
elif [[ $number -eq "5634" ]]; then
echo "You won the third prize."
else
echo "Try again."
fi
The script is executed two times. The “Try again” message is printed when the 6734 number is taken as the input value. The “You won the second prize” message is printed when the 3489 number is taken as the input value because it matches with the second “if” condition.
Example 3: Take Multiple Input Values Using Prompt
In the previous two examples, the single input is taken using each “read” command. But you can take multiple inputs in Bash using the “read” command. In this case, you have to pass the multiple variables with a space when taking multiple inputs using the “read” command. Create a Bash file with the following script that takes three inputs into three variables – $id, $batch, and $cgpa. The values of these variables are printed later.
echo "=====Enter student information with space====="
#Take multiple input values using a prompt message
read -p "ID, Batch, CGPA: " id batch cgpa
echo
echo "***Output based on the input values:***"
#Print the input values
echo "ID: $id"
echo "Batch: $batch"
echo "CGPA: $cgpa"
The following output appears after executing the script with the values of id=’01156788’, batch=42, and cgpa=3.97:
Conclusion
The uses of the prompt with the “read” command in the Bash script are shown in this tutorial using multiple examples. The benefits of using a prompt message with the “read” command is cleared after reading this tutorial.