Examples of using single and double quotes in bash

What’s the difference between single and double quotes in bash script? Let’s take examples to make it clear. This is a really problematic topic in which I constantly get confused. Keep in mind that single and double quotes are completely different characters and it have different meanings in bash. Try, if possible, to use them for their intended purpose, more correctly. Then it will help you to better remember their role.

Simple variable

First we create a variable

variable=apple

And then try to get the value inside using double quotes

echo "$variable"
...
output: apple

But if we use single quotes, the result will be different

echo '$variable'
...
output: $variable

So, we couldn’t get the value inside the variable. Command echo printed what was between single quotes.

The conclusion is: variables are expanded inside double quotes, and not expanded inside single quotes.

What if we mix them?

Let’s use single quotes inside double quotes

echo "'$variable'"
...
output: 'apple'

So, variable content and single quotes are both output.

The conclusion is: single quotes inside double quotes work as simple characters.

What about using double quotes inside single quotes?

echo '"$variable"'
...
output: "$variable"

So, double quotes don’t matter here. It works as simple characters. Single quotes as usual print content between them.

The conclusion is: double quotes inside single quotes work as simple characters.

Escape character

Single quotes preserves the literal meaning of each character.

echo '$(echo "Simple text")'
...
output: $(echo "Simple text")

But double quotes can interpret some special characters, such as $, @, *, !

echo "$(echo "Simple text")"
...
output: Simple text

It is important to understand what is content between quotes

echo ' \' '
...
Error

What’s the trouble? The first single quote is closed with the next single quote. But we have three single quotes. Bash can’t find the next single quote for the third one. So, bash interprets this as if we asked him first print ‘ \ ‘ and then single ‘. We should at least close it with a fourth quote.

echo ' \' ' '
...
output: \

The second pair of single quotes is empty, so it is ignored. If we want to print exactly \’, it is better to use double quotes

echo " \' "
...
output: \'

Example with ‘ \’ ‘ shows that single quote between single quotes can’t be escaped. Because single quotes interpret characters as is. But we know, that double quotes can interpret special characters. So, let’s take an example

echo " \" "
...
output: "

\ special character will always be interpreted when it matters. But when it is not significant, it works as a simple character

echo " \' "
...
output: \'

“\t \n” interpreted as a literal meaning inside double quotes. But we can use ANSI-C Quoting with $’ ‘ format

echo $'\t''tabulation'
...
	tabulation

But ANSI quoting is not interpreted inside double quotes

echo " $'\t' "'tabulation'
...
output: $'\t'tabulation

“ and $() construction’s are both works inside double quotes

echo "`echo First test`"
echo "$(echo Second test)"
...
output: First test
            Second test

What if we use a variable in the middle of the text?

echo "One$variableTwo"
...
output: One

So, bash thinks our variable is $variableTwo and since it is empty there is no output. We can use { } to determine variable directly.

echo "One${variable}Two"
...
output: OneAppleTwo

Leave a Reply

Your email address will not be published.