Friday, December 27, 2024
Google search engine
HomeGuest BlogsBash Single vs. Double Quotes: What’s the Difference?

Bash Single vs. Double Quotes: What’s the Difference?

Introduction

Single and double quotes are often used in Linux bash commands or scripts, especially when dealing with filenames. Although both quote types prevent globbing and word splitting, it is important to pay attention to the quotes you use. The differences between the quote types make them noninterchangeable in some cases.

In this tutorial, you will learn the difference between single and double quotes and see examples of how to use them.

Bash single vs. double quotes - what's the difference?Bash single vs. double quotes - what's the difference?

Prerequisites

  • A system running Linux.
  • Access to the terminal (Ctrl+Alt+T).

Bash Single vs. Double Quotes: What Are the Differences?

Use single (') and double quotes (") in bash shell scripts or when executing commands directly in the terminal to instruct the shell how to interpret certain characters. Since Bash treats single and double quotes differently, make sure to know the difference before writing a bash script or executing a command.

Single Quotes

The main point of using single quotes in Bash (') is to preserve the literal value of each character within the quotes. Single quotes strip any special value of a character and should be used when you want the input to retain its literal value without any data interpolation.

Single quotes are convenient when you don’t want to use the escape characters to change how Bash interprets the input string.

Double Quotes

On the other hand, double quotes (") preserve the literal value of all characters except for ($), (`), ("), (\), and the (!) character when history expansion is enabled.

The (!) character has no special meaning within double quotes when the shell is in POSIX mode, even if history expansion is enabled. History expansion is disabled if there is a (!) character within the double quotes, and it is escaped using a backslash.

Within double quotes, a backslash character (\) has a special meaning only when it is followed by ($), (`), ("), (\), or a newline character. If it is followed by any of those characters within double quotes, the backslash does not appear in the output. A double quote is allowed within double quotes if a backslash precedes it.

Double quotes also give the (*) and (@) characters special meaning when shell expansion is introduced using the ($) character. For example, ($@) passes the specified parameters separately, while ($*) passes all parameters as a single parameter.

The table below summarizes the basic differences between single and double quotes in the bash shell:

Double quotes Single quotes
Allow variable expansion. Prevent variable expansion.
Allow history expansion (if enabled) with the (!) character. Prevent history expansion.
Allow command substitution. Prevent command substitution.
Allow array access. Do not allow array access.
(*) and (@) can have a special meaning when encased in double quotes. (*) and (@) are always literals when encased in single quotes.
Can be wrapped around single quotes or double quotes. Single quotes have no special meaning within double quotes. Single quotes are not allowed inside single quotes.
Double quotes are treated literally within single quotes.
When ($, (`), ("), (\) are encased in double quotes, they can be escaped with \ to prevent their special meaning. When ($, (`), ("), (\) are encased in single quotes, they retain their literal values.

Bash Single vs. Double Quotes Examples

To better understand the differences between single and double quotes, refer to the sections below for hands-on examples and how to test them in bash. Either run the commands directly in the terminal or create a script and save it for later.

Example 1: Differences in Variable Output

The simplest way to see the differences between single and double quotes is to use them on a variable. Follow the steps below:

1. Declare a variable named example and assign a value:

example=20

2. Output the variable contents using the echo command.

With single quotes, the $example variable output does not get replaced with the variable value, but displays the variable name within the quotes, i.e., its literal form.

However, running the echo command with double quotes expands the $example variable and outputs the assigned value instead of printing the characters within the quotes.

Run the following commands to see the difference:

echo '$example'
echo "$example"
Example showing the difference between using single and double quotes to output a variable's contents.Example showing the difference between using single and double quotes to output a variable's contents.

The single quotes tell bash to print the characters specified within the quotes, i.e., the variable name. On the other hand, double quotes interpolate the input and display the variable’s value.

Note: The $ character causes shell expansion and instructs bash to replace all the characters after the dollar sign with the specified value if used within double quotes.

Example 2: Newline Character Interpretation

When encased in single quotes, a newline character (\n) is displayed in its literal form and not as a newline character. However, using double quotes shows the newline character in the output.

This can be an issue when using the bash printf command, as it does not automatically add newline characters in the output. Instead, the command requires you to add it manually.

For example:

Using single and double quotes in printf command to print a newline character.Using single and double quotes in printf command to print a newline character.

When using single quotes, the newline character is printed in its literal form, as a backslash (\) and (n). However, double quotes cause the shell to interpret it as a newline character, as seen in the example above.

Example 3: Use Quotes Within Quotes

If there are spaces between words in the contents of a variable, wrap the whole string in quotes, or the shell throws an error. The error occurs because the shell interprets each word as a new argument when there are no quotes.

However, if you want to use quotes in the input, combine single and double quotes. Single quotes are allowed within double quotes, and bash accepts such variable values.

For example:

Using quotes within quotes in bash.Using quotes within quotes in bash.

The shell accepts the value and prints the variable contents with the single quotes around the specified word.

Similarly, the shell does not accept double quotes in a variable unless you encase them in single or double quotes.

Example 4: Including Spaces in Filenames

The shell uses spaces as separators between the specified arguments. However, if you are dealing with a file that contains spaces or certain characters in the filename, you must use quotes to instruct the shell to see it as a single file.

In the following example, we use quotes with the mv command to rename a file that contains a space in the filename:

mv 'new file' new_file
Renaming a file with a space in the filename.Renaming a file with a space in the filename.

The command changes the space in the filename to an underscore. Running the command without quotes results in the following error:

Error that occurs if quotes aren't used for files with spaces in filenames.Error that occurs if quotes aren't used for files with spaces in filenames.

Example 5: Using Arrays

Quotes are necessary when working with arrays in Bash. However, only double quotes are allowed since single quotes do not produce the expected output.

Follow the steps below to create a script and see the difference:

1. Open the terminal (Ctrl + Alt + T) and create a new bash script. Use a text editor such as vi/vim:

vi script.sh

2. Enter the following code to create a basic array:

#!/bin/bash
declare -a sport=(
[0]=tennis
[1]=basketball
[2]=soccer
[3]=rugby
)
echo "${sport[@]}"

After declaring the array, display all its elements using the (@) symbol. The array must be enclosed in double quotes to obtain the expected output. Enclosing the array in single quotes only prints ${sport[@]} in the output.

3. Save the script and exit vi:

:wq

4. Run the Bash script to test if it works:

bash script.sh
Running a script containing an array in bash.Running a script containing an array in bash.

Changing the double quotes in the script to single quotes results in the following output:

Bash array not working with single quotes.Bash array not working with single quotes.

The script doesn’t print the array elements but outputs the array name.

Conclusion

This tutorial explained the difference between double and single quotes in bash and showed examples for using both quote types. After learning the differences, you should know how to instruct the shell on how to interpret the input strings and whether to print the input as is or to interpolate data.

Learn more about bash in our tutorial for bash string comparison, or learn the difference between bashrc and bash_profile.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments