Different types of quotes are used in Bash to define a string data or execute a command. These are single quotes, double quotes, and back quotes or backticks. When the “echo” command is used with the single quotes, each character of the string is parsed without any expansion. That means if any Bash variable is enclosed with the single quotes (”), the variable name is printed in place of the value of the variable. The uses of single quotes in the Bash script are shown in this tutorial.
Different Examples of Single Quotes in Bash
Different uses of single quotes are shown in this part of the tutorial.
Example 1: Read a Variable Using Single Quotes
Create a Bash file with the following script that takes an input from the user. The name of the variable is printed in place of the value of the variable if the variable is enclosed with the single quotes. In the script, the variable is printed by enclosing with single quotes and enclosing without the single quotes.
#Take the input from the user
echo "Do you like music?"
read answer
#Print the variable within single quotes
echo "Variable with single quotes:"
echo 'Your answer is $answer'
#Add newline
echo
#Print the variable without single quotes
echo "Variable without single quotes:"
echo 'Your answer is '$answer
The following output appears after executing the script. The name of the $answer variable is printed when the “echo” command is used with the single quotes. The value of the $answer variable is printed when the variable is used without any quote in the “echo” command:
Example 2: Read Multiple Variables with Other Strings with Single Quotes
Create a Bash file with the following script that takes two input values from the user. In the script, the values of two variable are printed with the other string value that is enclosed with the single quotes.
#Read two input values from the user
read -p "Enter the participant name: " participant
read -p "Enter the score value: " score
#Print a single quote within the single quotes
echo $participant\''s score is '$score
The following output appears after executing the script with the participant value of “Farheen” and the score value of 95. The values of the $participant and $score variables are printed with the string value of “s score is” using the “echo” command. The final string value, the “Farheen’s score is 95”, is printed in the output:
Example 3: Read the Variable Using the “$” Symbol
Create a Bash file with the following script that displays a menu of three options. The user can select any option using any number from 1 to 3. The $placeName variable is set based on the selected numeric value. Next, the value of the $placeName variable is printed using the “$” symbol in the “echo” command.
#Display a menu
echo "Popular places:"
printf "1. Sonargaon\n"
printf "2. Cox's Bazar\n"
printf "3. Kuakata\n"
#Select a place based on numeric value
read -p "Select your favorite place[1-3]: " place
#Set the place name based on the selection
case $place in
1) placeName="Sonargaon" ;;
2) placeName="Cox's Bazar" ;;
3) placeName="Kuakata" ;;
*) placeName="Unknown" ;;
esac
#Print a single quote within the single quotes using '$'
echo $'Your favorite place is '$placeName'.'
The following output appears after executing the script if the second menu option is selected by pressing 2. The $placeName variable is set to “Cox’s Bazar” which is printed in the output:
Example 4: Using the Single Quotes in the “Printf” Command
Create a Bash file with the following script that takes a person’s name, profession, and email from the user and the input values using the “printf” command where the single quotes are used to mention the type of the variable. The variables are enclosed by the double quotes. The “%s\n” is used in the “printf” command to print the value of each variable with a newline.
read -p "Enter your name: " name
read -p "Enter your profession: " profession
read -p "Enter your email: " email
#Print the input values
echo 'Personal details:'
printf '%s\n' "$name" "$profession" "$email"
The following output appears after executing the script if the input values are “Fahmida Yesmin”, “Teacher”, and “[email protected]”:
Conclusion
The purposes of using the single quotes are explained in this tutorial using multiple examples of Bash script. Declaring a string variable is the main purpose of the single quotes but it can’t be used to access the values of the variable. The method of using single quotes with both “echo” and “printf” commands is shown in this tutorial.