- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash Single vs. Double Quotes What's Difference
If you've spent any time using Bash, you've probably noticed that there are two types of quotes you can use when defining variables or passing arguments to commands: single quotes (' ') and double quotes (" ").
At first glance, these quotes might seem interchangeable, but there are actually some key differences between them that can impact behavior of your Bash commands. In this article, we'll explore those differences and explain when you should use single quotes, when you should use double quotes, and what happens when you mix them up.
Defining Single and Double Quotes
Before we dive into differences between single and double quotes, let's define what they are and how they work.
Single Quotes − Enclosing a string of text in single quotes will cause Bash to interpret everything inside quotes as a literal string, with no special meaning to any of characters enclosed.
Example
echo 'Hello World!'
Output
Hello World!
In example above, single quotes ensure that exclamation point at end of string is interpreted as a literal character, rather than a special character that might have some other meaning in Bash.
Double Quotes − Enclosing a string of text in double quotes will cause Bash to interpret certain characters inside quotes in a special way. For example, Bash will substitute any variables inside quotes with their values, and it will interpret certain escape sequences, such as
(newline), \t (tab), and \e (escape).
Example
name='John' echo "Hello $name!"
Output
Hello John!
In example above, Bash substitutes value of $name variable (which is "John") into string enclosed in double quotes.
Single Quotes vs. Double Quotes: Differences
Now that we know how single and double quotes work, let's explore differences between them.
Variable Substitution
As we saw in previous example, Bash substitutes value of variables enclosed in double quotes, but not in single quotes. This means that if you want to include value of a variable in a string, you should enclose string in double quotes.
Example
name='John' echo "Hello $name!" echo 'Hello $name!'
Output
Hello John! Hello $name!
In second example above, variable $name is not substituted because it is enclosed in single quotes.
Command Substitution
You can also use quotes to enclose commands, which allows you to substitute output of command into a string. However, there is a difference between how single and double quotes handle command substitution.
When you enclose a command in double quotes, Bash will execute command and substitute its output into string. When you enclose a command in single quotes, Bash will treat command as a literal string, and it will not execute command or substitute its output.
For example −
$ echo "The current directory is: $(pwd)" The current directory is: /home/user $ echo 'The current directory is: $(pwd)' The current directory is: $(pwd)
In first example above, command pwd is executed, and its output is substituted into string enclosed in double quotes. In second example, command pwd is treated as a literal string because it is enclosed in single quotes.
Special Characters
As we mentioned earlier, Bash interprets certain escape sequences, such as
and \t, inside double quotes but not inside single quotes. This means that if you want to include special characters in a string, you should enclose string in double quotes.
For example −
$ echo "This is a string
with a newline character" This is a string with a newline character
In example above,
escape sequence is interpreted as a newline character because it is enclosed in double quotes. If we had enclosed string in single quotes, Bash would have interpreted
as a literal backslash followed by letter n.
Quoting Characters
Finally, it's worth noting that you can use quotes to include characters that would otherwise be interpreted by Bash as special characters. For example, if you want to include a literal $ or \ character in a string, you can enclose string in single quotes.
Example
echo '$5.00' echo '\home\user'
Output
$5.00 \home\user
In first example above, $ character is treated as a literal character because string is enclosed in single quotes. In second example, backslashes are also treated as literal characters because string is enclosed in single quotes.
Mixing Single and Double Quotes
Sometimes, you might want to include both single and double quotes in a string. In these cases, you can use a combination of quotes to achieve desired result.
Example
echo "He said, 'Hello World!'" echo 'The cost is $"5.00"'
Output
He said, 'Hello World!' The cost is $"5.00"
In first example above, we use double quotes to enclose entire string, but we use single quotes to enclose nested string "Hello World!". In second example, we use single quotes to enclose entire string, but we use double quotes to enclose nested string $"5.00".
In addition to differences we've already discussed, there are a few other nuances to consider when using single and double quotes in Bash.
First, it's worth noting that quotes can be nested within each other. For example, you can enclose a string in double quotes and then enclose a nested string within single quotes.
Example
echo "He said, 'My name is John.'"
Output
He said, 'My name is John.'
In this example, we use double quotes to enclose entire string, but we use single quotes to enclose nested string "My name is John.".
Second, it's important to be aware of how Bash treats whitespace characters (such as spaces and tabs) inside quotes. When you enclose a string in double quotes, Bash preserves any whitespace characters inside quotes. When you enclose a string in single quotes, Bash treats all whitespace characters as literal characters.
Example
name='John Smith' echo "Hello, $name" echo 'Hello, $name'
Output
Hello, John Smith Hello, $name
In first example above, Bash preserves space character between "John" and "Smith" because string is enclosed in double quotes. In second example, Bash treats tab character as a literal character because string is enclosed in single quotes.
Finally, it's worth mentioning that quotes are not always necessary in Bash. For example, if you're defining a variable that contains a string with no special characters, you can omit quotes altogether.
Example
message=Hello echo $message
Output
Hello
In this example, we define variable "message" to contain string "Hello" without enclosing it in quotes. When we later echo variable, Bash prints string "Hello" without any issues.
Conclusion
In summary, main difference between single and double quotes in Bash is that double quotes allow for variable substitution, command substitution, and interpretation of certain escape sequences, while single quotes treat everything inside them as a literal string. When quoting strings in Bash, it's important to choose appropriate type of quotes based on what you want to achieve. If you're unsure which type of quotes to use, it's usually safe to use double quotes, as they allow for most flexibility.