What is the difference between single quotes and double quotes in bash

When you are writing shell scripts or typing out commands in bash, you might have noticed that you can use either the single quotes (‘) or the double quotes (“) with most commands. This is true not just with scripts but with all bash commands as well. But there is a difference between the two types of quotes, and how the bash interprets it.

In the simplest of words, the single quotes will interpret the enclosing text literally and will not interpolate any part of the text such as variables. The double quotes will interpret the enclosing string and transform the text when appropriate such as variables, escape characters, backticks etc. We will take a look at some examples later in order to clarify this further.

Double Quotes

When you enclose a string with in the double quotes, the literal values of all the characters are preserved except for the $ (dollar sign), ` (backtick), \ (backslash) and ! (exclamation mark). These 4 characters have special meaning is interpreted differently.

The dollar sign ($) indicates that the characters following it is a variable name and should be replaced with the value of that variable. The backslash (\) is used to escape other characters, and has special only when followed by certain other characters such as $ (dollar), \ (backslash), ” (double quotes), ` (backtick) or the new line.

$ echo "$PATH"

Single Quotes

When you enclose a string in single quotes, then all the characters in the string are interpreted literally and do not have any special meaning. This is useful when you do not want to use the escape characters to change the way the bash interprets the input string.

$ echo '$PATH'

double and single quotes in bash

You can use the simple echo command to see the difference between the two quotes. If you do not use any quotes then the string will be interpreted, just as you have used the double quotes.