Quoting in Bash Scripting

In this article, we'll take an in-depth look at Bash quotations.
Quoting in Bash Scripting

On this page

Quoting often presents a stumbling block for Linux users, particularly those just starting out. In the Bash shell, characters like $ and * have special meanings, representing variables and wildcards respectively.

There are occasions, though, when the goal is to display these characters exactly as they are, without invoking their special functions. This is where quotations come in handy.

Bash supports three main types of quotations: single quotes, double quotes, and backslashes. Each allows you to control precisely how Bash interprets special characters.

In this article, we'll take an in-depth look at Bash quotations. We'll explain when and how to use single, double, and backslash quotes through concrete examples. You'll learn the exact escape sequences for each type of quote and see the correct usage demonstrated.

By the end, you'll have mastered quotation in Bash scripting. You'll know how to display special characters without triggering their built-in functionality.

Using Double Quotes

Double quotes (") in bash allow partial interpretation of special characters. Some expansion occurs within double quotes, while other characters are treated as literals.

For example, running the following command will expand the $USER variable to print the current username.

$ echo "$USER"

traw

The following example uses backticks inside double quotes for command substitution:

$ echo "The time is `date`."

The time is Wed Feb 28 22:44:37 SAST 2024.

This will concatenate the string inside the quotations with the output of the date command.

Specifically, double quotes perform the following expansions:

  • Variable expansion - "$HOME" expands the home directory path.
  • Command substitution - "Today is $(date)." or The time is date." runs date command and substitutes output.

Splitting long strings over multiple lines using backslash:

$ echo "This is a long string that \
spans multiple \
lines"

This is a long string that spans multiple lines

However, double quotes do NOT perform file globing. For example, running the following command will treat *.txt as a literal string.

$ echo "*.txt"

*.txt

Without the double quotes, the command expands *.txt wildcard and prints matching filenames:

$ echo *.txt

data.txt hello.txt new.txt

Using Single Quotes

Single quotes (') in bash disable interpretation and expansion of all special characters between them. Anything within single quotes is treated as a literal string. When I mention that characters are "treated literally" in the context of quoting in Bash, it implies that the characters within the quotes are interpreted exactly as they appear, without any special interpretation or expansion.

Let’s take a look at this example:

$ echo '$USER'

$USER

Notice, that the string inside the single quotes was printed literally instead of expanding the variable. Here are some more examples:

# Treats *.txt as string instead of expanding wildcard 
$ echo '*.txt'

# Command substitution is disabled
$ echo 'Today is $(date)'

Specifically, single quotes disable:

  • Variable expansion - '$HOME' prints $HOME instead of the home directory path.
  • File globing - './*' does not expand wildcards.
  • Command substitution - '$(uname -r)' runs as plain text.

This allows you to print special characters like $, *, backticks, and \ literally.

💡
When should you use single quotes? Use them when you want to completely disable the interpretation of special characters. For example, to print a file glob pattern without actually expanding it.

The key is that with single quotes, NO expansion or interpretation occurs. Everything between them is displayed exactly as-is. This prevents unwanted evaluations.

Using Backslashes

The backslash (\) in Bash escapes characters. Placed before another character, it removes any special meaning, causing Bash to interpret it literally instead.

For example:

echo \$PATH

The above example will print $PATH instead of expanding it to a variable.

Another example of printing text with quotes:

$ echo \"Hello, Linux folks\"

"Hello, Linux folks"

This is also the same as:

$ echo '"Hello, Linux folks"'

Common Uses of Backslashes

  • Escape dollar signs, backticks, quotes, and other special characters:
    • \$ - Print $ literally
    • \date\ - Use backticks as normal text
  • Continue statements or commands over multiple lines.
  • Escape spaces in filenames.
  • Escape wildcard symbols to prevent globing:
💡
When should you use backslashes? Use them when you want to treat a special character as regular text instead of allowing bash to interpret it.

The backslash escape character provides flexibility in bash to explicitly enable or disable special meaning as needed.

Conclusion

And there you have it - a comprehensive guide to quoting in Bash scripting. We covered the three main types of quotes: double, single, and backslashes. You learned when to use each one to control the interpretation of special characters.

Double quotes enable partial expansion, single quotes disable all expansion, and backslashes let you escape individual characters. Mastering Bash quoting gives you precise control over your scripts.

You can now display special symbols like $ and * literally, when necessary, while also utilizing their built-in functionality at other times. Quoting no longer has to be intimidating.

Whether you are an experienced scripter or just starting out, I hope you've found this overview helpful. Using the correct quotes at the appropriate times will make your Bash scripts more readable, robust, and reliable. Your newly acquired quoting skills will serve you well as you navigate the Linux command line!

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!