FUNDAMENTALS A Complete Guide for Beginners
As a Linux user, it’s essential to pay attention to the quotes you use and their different behaviors in Bash. At first glance, these quotes may look similar, but their differences can impact the shell interpretation differently. In this article, I will explore Bash single quotes vs double quotes in a more detailed form.
Key Takeaways
- Learning about the basic difference between single and double quotes.
- Exploring several cases to see the impact of single and double quotes on Bash.
Basic Difference Between Single Quotes & Double Quotes in Bash
The table below summarizes the basic differences between single and double quotes in the bash shell:
Single Quotes | Double Quotes |
---|---|
Don’t perform variable expansion. | Perform variable expansion. |
Don’t perform command substitution. | Perform command substitution. |
Prevent history expansion. | Allow history expansion. |
Less flexible. | More flexible. |
Single quotes cannot be wrapped around single quotes. | Double quotes can be wrapped around both single or double quotes. |
Don’t allow any array access. | Allow array access. |
Bash treats special characters like ‘$’, ‘\’, ‘!’ etc within single quotes as literal characters. | The special characters like ‘$’, ‘\’, ‘!’ etc within double quotes can be escaped with a backslash (\) to prevent their special meaning. |
3 Cases to Differentiate Between Single and Double Quotes in Bash
As you already know what single and double quotes are, let’s look into the dissimilarities between them in the following section:
Case A: Variable Interpretation
Bash expands the value of the variables only when they are enclosed in double-quotes. If you include the value of a variable in a string and enclose them in single quotes, Bash would not substitute the values. Let’s have an example:
Steps to Follow >
➊ Go to Ubuntu Terminal, open a script in the Nano text editor by writing the following command:
nano doublevar.sh
- nano: A text editor.
- doublevar.sh: This is a script. Here, I have named the script ‘doublevar.sh’. You can name any of your choices.
➋ Now, write the following script inside the editor:
Script (doublevar.sh) >
#!/bin/bash
#Declaring variable
website='LinuxSimply'
#Displaying the output
echo "The project is $website."
Here, in #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. In the line website=’LinuxSimply’, I have declared a variable ‘website’. Next, in the line echo “The project is $website.”, the echo command displays the output.
➌ Then, press CTRL+S to save the file & press CTRL+X to exit.
➍ After that, use the command below to make the script executable:
chmod u+x doublevar.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- doublevar.sh: The file which you want to make executable.
➎ Finally, run the script by the following command:
./doublevar.sh
The image shows that Bash has expanded the value of the variable ‘website’ when they are enclosed in double-quotes.
Again, write the following script inside the editor:
Script (singlevar.sh) >
#!/bin/bash
#Declaring variable
website='LinuxSimply'
#Displaying the output
echo 'The project is $website.'
Here, in the line website=’LinuxSimply’, I have declared a variable ‘website’. Next, in the line echo ‘The project is $website.’, the echo command displays the output as a literal value.
Now, run the script by the following command:
./singlevar.sh
The above image interprets that Bash performs no variable substitution when they are enclosed in single quotes.
Case B: Command Substitution
Another variation between single and double quotes you will find is the command substitution. By enclosing commands within quotes you can substitute the output of the command into a string. Here’s how single and double quotes handle command substitution:
Script (command1.sh) >
#!/bin/bash
echo "The contents of current directory: $(ls)"
Now, run the script by the following command:
./command1.sh
From the image, you can see that Bash executes the ls command encased in double quotes and substitutes its output into strings. Here, the strings command1.sh, command2.sh, linuxsimply, my file.sh, navigate.sh, and Ubuntu demonstrates the content list of my current working directory ’Desktop’.
Again, write the following script inside the editor:
Script (command2.sh) >
#!/bin/bash
echo 'The contents of current directory: $(ls)'
Now, run the script by the following command:
./command2.sh
From the image, you can see that Bash treats the ls command as a literal string when encased in single quotes and doesn’t substitute its output.
Case C: Accessing Array Elements
While dealing with Bash arrays, quotes play an essential role in interpreting the elements of the array. Here’s how single and double quotes perform while accessing array elements:
Script (array1.sh) >
#!/bin/bash
#Declaring an array
declare -a colors=(
[0]=red
[1]=white
[2]=blue
[3]=black
[4]=purple
)
#Displaying the output
echo "The colors are: "${colors[@]}""
Here, the line ‘declare -a colors=(…)’ declares an array ‘colors’ where the content enclosed in ‘( )’ symbolizes the array elements according to the index number. Then, in the line echo “The colors are: “${colors[@]}””, the echo command prints all the elements of the ‘colors’ array.
Now, run the script by the following command:
./array1.sh
From the image, you can see all five elements of the array ‘colors’. It indicates that Bash interprets the array elements within double quotes.
Again, write the following script inside the editor:
Script (array2.sh) >
#!/bin/bash
#Declaring an array
declare -a colors=(
[0]=white
[1]=yellow
[2]=black
[3]=blue
[4]=purple
)
#Displaying the outputs
echo '${colors[@]}'
Here, the line ‘declare -a colors=(…)’ declares an array ‘colors’ where the content enclosed in ‘( )’ symbolizes the array elements according to the index number. Then, in the line echo ‘${colors[@]}’, the echo command prints the name of the ‘colors’ array.
Now, run the script by the following command:
./array2.sh
The above image depicts that Bash prints only the array name rather than the array elements when the strings are enclosed within single quotes.
Conclusion
As single and double quotes are different quoting mechanisms in Bash, you have to understand the individual working behavior of these quotes. Hope the above demonstration will help you a lot to get a clear concept of single quotes vs. double quotes in Bash and apply them accordingly.
People Also Ask
Related Articles
- What is Quoting in Bash? [5 Hacks You Didn’t Know]
- Understanding Single Quotes in Bash [2 Examples With Cases]
- Understanding Double Quotes in Bash [3 Examples]
- Escape Quotes in Bash
<< Go Back to Bash Quotes | Bash Scripting Tutorial