Bash Quotes

While doing Bash scripting, especially when dealing with variables and filenames, it’s very important to pay attention to the appropriate use of quotes for robust and efficient Bash scripts. In this article, I’ll demonstrate the context of  Bash quotes including their types and several examples. Let’s explore!

Key Takeaways

  • Learning how quotes are used in Bash.
  • Learning about various types of quotes in Bash.

What Are Quotes in Bash?

Quotes are the fundamental elements in Bash that enclose and symbolize characters or string literals. These can prevent parameter expansion and disable special treatment for special characters. Besides, you can use quotes to make Bash consider the enclosed content within quotes as a single item, instead of determining them as separate items. Additionally, quotes are a great element to prevent Globbing (Expanding wildcard characters into matching paths) problems in Bash.

Strings & Variables Within Quotes

When you are working with just simple texts, strings, or characters, whether you use single quotes or double quotes, it doesn’t matter at all. For both cases, you will see the same type of output as the example below:

Using Single Quotes >

echo 'Hello, LinuxSimply!'

Using Double Quotes >

echo "Hello, LinuxSimply!"

Output >

Hello, LinuxSimply!

Alongside, when you define any variable by enclosing it with single quotes, Bash doesn’t consider it as a variable. So, always try to define variables within double quotes.

Types of Quotes in Bash

You can append quotes in three basic forms. These mechanisms are discussed below:

A. Bash Single Quotes

In Bash, single quotes (‘ ’) are an important quoting mechanism that holds the literal value of every character inside the quotes. This is very convenient when you want to avoid using escape characters for data interpolation.

This is the simplest form of quoting as it preserves all special characters, including newlines, spaces, and tabs. Above all, whenever you enclose any content inside the single quotes, Bash performs the task without any variable expansion or command substitution.

B. Bash Double Quotes

When you encase any string in double quotes (“ ”), Bash expands the value of the variable inside the string as well as performs command substitution. Generally, double quotes can store values of all characters except ‘$’, ‘\’, ‘`’, ‘’.

When history expansion is enabled, double quotes don’t preserve the character !’. But if history expansion is disabled, you can escape the character !’ by using a backslash (\). In addition, by preceding a backslash, you can use a double quote within double quotes very easily.

C. Bash Escape Quotes

Escape characters are generally used to remove the special meaning of a single character. You can define a Bash escape character by using a non-quoted backslash (\) that is enough to retain the literal value of the next characters followed by this except for the newline tasks.

The special character ‘$’ symbolizes any defined variable in Bash. But when you use a backslash before the ‘$’ symbol, Bash ignores it and prints only the variable name instead of its value. Run the commands below to see the effects of an escape character:

Without Escape Quote >

var1=`pwd`
echo $var1
EXPLANATION
  • var1=`pwd`: Declares the variable var1 with its value `pwd`.
  • echo $var1: Prints the value of the variable var1 by capturing the output of the command pwd (present working directory). Here, the location of my current working directory is /home/nadiba/Desktop.

Output without escape character

From the image, you can see that the ‘$’ symbol before the variable name ‘var2’ without preceding any escape character (\) prints the variable’s value.

With Escape Quote >

var2=`pwd`
echo \$var2
EXPLANATION
  • var2=`pwd`: Declares the variable var2 with its value `pwd` that indicates the present working directory.
  • echo \$var2: Prints only the variable name var2 preceding with the ‘$’ symbol.

Output with escape character (\)

From the image, you can see that the ‘$’ symbol before the variable name ‘var2’ preceding an escape character (\) ignores the variable’s value.

Bash Single VS Double Quotes

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 Examples of Quotes in Bash

To clearly understand the quotes in Bash, follow the examples below and either run the commands in the terminal or create a script, put the commands, and save it.

Example 1: Printing Variable Output Using Quotes in Bash

Follow the steps to print out the variable outputs by enclosing them within single and double quotes:

Steps to Follow >

➊ Open your Ubuntu Terminal.

➋ Now, declare a variable and assign a value to it like the following:

country=Bangladesh

➌ Then, run the variable contents using the echo command with both single quotes and double quotes individually.

echo 'This is $country'
echo "This is $country"

Printing variable outputs

From the above image, you can see that when I have run the variable within single quotes, it tells Bash to display only the specific characters within the single quotes. On the contrary, the variable within double quotes dictates the assigned value of the variable.

Example 2: Encasing Quotes Within Quotes

While assigning values to a variable, if there are any spaces between words, Bash interprets each word as an argument. So, to avoid this, encase the whole string in quotes. Otherwise, Bash will count it as an error.

However, to use quotes within quotes in the input, you need to combine single and double quotes like the following:

var1="Hello, 'Linux' world."
echo $var1
var2='Hello, "Linux" world.'
echo $var2

Encasing quotes within quotes

The above snapshot depicts that Bash accepts the single quotes within double quotes and vice versa.

Example 3: Interpreting Newline Character (\n)

Run the following commands to interpret the newline character (\n) by encasing it in both single and double quotes:

echo 'This is the first line.\nThis is the new second line.'
echo -e "This is the first line.\nThis is the new second line."

Interpreting Newline character (\n)

In the above image, while using single quotes, Bash prints the newline character in its literal form instead of printing it as a newline character. However, while using double quotes, Bash interprets it as a newline character.

Conclusion

Quotes in Bash are an essential feature for handling Bash interpretation and text and string management. To conclude, choosing a proper quote type and understanding how to use them is the key factor for writing robust and reliable Bash scripts.

People Also Ask

Does Bash support variable expansion within single quotes?
No, Bash doesn’t support variable expansion within single quotes. Use double quotes (“ ”) instead for variable expansion.

How to include a dollar sign ($) as a literal character within a double-quoted string?
Escape the dollar sign using a backslash (\$) within a double-quoted string to include it as a literal character.

How to prevent word splitting issues in Bash?
While working with variables containing spaces or special characters, use proper quoting, especially double quotes, to prevent word splitting as well as globbing issues in Bash.

Can I include a literal single quote within a single-quoted string?
Yes, you can either escape the single quote character (‘) with a backslash (\) within the single-quoted string or close the single-quoted string, insert the single quote character (‘), and then reopen the single-quoted string.

Related Articles


<< Go Back to Bash Scripting Tutorial

5/5 - (4 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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