Difference Between Single and Double Quotes in Shell Script and Linux
Last Updated :
17 Nov, 2020
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'

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'

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]}'

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"'
Similar Reads
Shell Scripting - Difference between Korn Shell and Bash shell Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year bac
3 min read
Bash Script - Difference between Bash Script and Shell Script In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capabl
4 min read
Difference Between a Script file and a Binary file In this tutorial, we will learn what a script file and a binary file are and what's the difference between a Script file and a Binary file. Binary File A binary file is a file in which the content of the file is in binary format, the file data is not human-readable. Binary files contain formatted in
4 min read
Explain the Difference Between shell_exec() and exec() Functions In this article, we will learn about the shell_exec() & exec() functions in PHP. As we know that in order to execute a command in a system, we need the shell of the respective operating systems, but if we need to execute the same command with the help of a programming language like PHP, then we
4 min read
Difference between Thompson Shell and POSIX Shell The Thompson Shell and the POSIX Shell are Unix command-line interpreters, however, they are different in their uses and were created at different time of Unix advancement. The Thompson Shell is one of the first Unix shells developed in the first half of the 1970 s and provided basic functionalities
5 min read