Difference Between Single and Double Quotes in the Bash Shell?

Enclosing text in double quotes is clearly standard practice on the terminal, especially when dealing with files or path that have spaces in the names, but how do we know whether to use single or double quotes? Let’s take a look at the difference, and when should we use one vs the other.

The general rule is that double quotes still allow expansion of variables within the quotes, and single quotes don’t. Let’s learn about the difference in detail.

Echo Sample

The explanation involves a simple variable a as well as an indexed array arr. If we set,

a=apple      # a simple variable
arr=(apple)  # an array with a single element

and then echo the expression in the second column, we would get the result / behavior shown in the third column. The fourth column explains the behavior.

# | Expression  | Result      | Comments
 1 | "$a"        | apple       | variables are expanded inside ""
 2 | '$a'        | $a          | variables are not expanded inside ''
 3 | "'$a'"      | 'apple'     | '' has no special meaning inside ""
 4 | '"$a"'      | "$a"        | "" is treated literally inside ''
 5 | '\''        | **invalid** | can not escape a ' within ''; use "'" or $'\''
 6 | "red$arocks"| red         | $arocks does not expand $a; use ${a}rocks to preserve $a
 7 | "redapple$" | redapple$   | $ followed by no variable name evaluates to $
 8 | '\"'        | \"          | \ has no special meaning inside ''
 9 | "\'"        | '           | \ is interpreted inside ""
10 | "\""        | "           | \ is interpreted inside ""
11 | "*"         | *           | globbing does not work inside "" or ''
12 | "*"         | *           | globbing does not work inside "" or ''
13 | "`echo hi`" | hi          | `` and $() are evaluated inside ""
14 | '`echo hi`' | `echo hi`   | `` and $() are not evaluated inside ''
15 | '${arr[0]}' | ${arr[0]}   | array access not possible inside ''
16 | "${arr[0]}" | apple       | array access works inside ""

Above table shows very clearly how quotes can differ the output involved.

Quotes with Simple Text

If we simply enclose a few words of text, it really doesn’t matter which one is used, as they will work exactly the same. For example, these two commands will both create a directory named Avoid Errors:

mkdir "Avoid Erros"
mkdir 'Avoid Erros'

More experienced types will probably note that we could also use mkdir Avoid\ Errors if we wanted.

Here is another example,

MYVAR=sometext echo "double quotes gives you $MYVAR" echo 'single quotes gives you $MYVAR'

will give this:

double quotes gives you sometext single quotes gives you $MYVAR

Single and Double Quotes Difference for Backslash

The bash script:


echo 'single quotes of backslash \\ ?'
echo "double quotes of backslash \\ ?"

The result of above script will be:

single quotes of backslash \\ ?  
double quotes of backslash \ ?


As explained above, the syntax $'string' specifies a C-style string which includes magic escaped characters, such as \n for a newline. $"string" is for I18N expansion, which has no such magic escapes.

Note that these are distinct from the more common "string" (weak quoting) and 'string'(strong quoting).

So, use of single and double quotes needs to be done wisely as they can alter the program flow.

You May Also Like

About the Author: Miguel

I started this tech blog back in 2011 as a place to write down processes I took to fix my client systems and network. Now I write some tips and tricks to help others with the tech issues that one might encounter.

Leave a Reply

Your email address will not be published. Required fields are marked *