Bash Single Vs Double Quotes [3 Cases to Differentiate]

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
EXPLANATION
  • 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.

Opening script in Nano editor

➋ 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."
EXPLANATION

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
EXPLANATION
  • 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.

Adding executable permission to the script

➎ Finally, run the script by the following command:

./doublevar.sh

Output of variable expansion within double quotes

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:

You can follow the Steps 3 & 4 of Case A, to save & make the script executable.

Script (singlevar.sh) >

#!/bin/bash

#Declaring variable
website='LinuxSimply'

#Displaying the output
echo 'The project is $website.'
EXPLANATION

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

Output of no variable expansion within single quotes

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:

You can follow the Steps of Case A, to save & make the script executable.

Script (command1.sh) >

#!/bin/bash

echo "The contents of current directory: $(ls)"

Now, run the script by the following command:

./command1.sh

Output of command substitution within double quotes

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:

You can follow the Steps of Case A, to save & make the script executable.

Script (command2.sh) >

#!/bin/bash

echo 'The contents of current directory: $(ls)'

Now, run the script by the following command:

./command2.sh

Output of no command substitution within single quotes

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:

You can follow the Steps of Case A, to save & make the script executable.

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[@]}""
EXPLANATION

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

Output of interpretation of array elements within double quotes

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:

You can follow the Steps of Case A, to save & make the script executable.

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[@]}'
EXPLANATION

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

Output of no interpretation of array elements within single quotes

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

Does Bash support nesting single and double quotes directly?
No, you cannot directly nest single and double quotes within each other in Bash. You should escape one of the quotes to make it work.

Which type of quotes are safer to prevent issues related to user input and file paths?
Double quotes are comparatively safer than single quotes to prevent unintended issues related to user input and file paths.

Does Bash allow backticks (`) within single quotes or double quotes?
Yes, Bash allows backticks (`) within single quotes (treated as literal characters) or double quotes (interpreted as command substitution).

Related Articles


<< Go Back to Bash Quotes | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment