Open In App

Difference Between Single and Double Quotes in Shell Script and Linux

Last Updated : 17 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Single quotes and double quotes are both functional in Linux while working with shell scripts or executing commands directly in the terminal but there is a difference between the way the bash shell interprets them. 

Single quotes:

Enclosing characters in single quotation marks (‘) holds onto the literal value of each character within the quotes.  In simpler words, the shell will interpret the enclosed text within single quotes literally and will not interpolate anything including variables, backticks, certain \ escapes, etc. No character in the single quote has special meaning. This is convenient when you do not want to use the escape characters to change the way the bash interprets the input string.

Double quotes:

Double quotes are similar to single quotes except that it allows the shell to interpret dollar sign ($), backtick(`), backslash(\) and exclamation mark(!). The characters have special meaning when used with double quotes, and before display, they are evaluated. A double quote may be used within double quotes by preceding it with a backslash.

Examples

1. In the below-mentioned case, the test is a variable that is initialized with 10. The dollar sign ($) indicates that the characters following are a variable name and should be replaced with the value of that variable which in this case is 10. When the $test is enclosed within single quotes, then the text inside is retained and the value does not get displayed. The $test does not get interpolated. But when it is closed within double quotes, then the $test is evaluated and the value of the variable which is 10 is printed.

test=10
echo "$test"
echo 'test'

single/double quotes in linux example

2. In the below-mentioned case, when \n is used within double quotes, it gets interpreted as a newline but when it is used within single quotes, \n is displayed along with other text in the same line.

printf "k\\nk"
printf 'k\\nk'

single/double quotes in linux example

3. In the below-mentioned case, when ${array[0]} is enclosed within single quotes, it gets evaluated and 10 is printed, as it is the 0th element of the array but when enclosed within single quotes, the literal identity of $ is retained and it does not get evaluated.

array=(10) #an array with a single element at index 0
echo "${array[0]}"
echo '${array[0]}'

single/double quotes in linux example

4. In the below-mentioned case, Single quotes have no special meaning when it is enclosed within double quotes, and hence $a gets evaluated even when it is within single quotes. But when double quotes are enclosed within single quotes, then it is treated literally and $a does not get evaluated even when it is inside double-quotes.

a=10
echo "'$a'"
echo '"$a"'

single/double quotes in linux example


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads