Difference between single and double quotes in Bash



Difference between single and double quotes in Bash

Difference between single and double quotes in Bash

Discover the difference between single and double quotes in Bash. What it means to have a single quote in shell script, and double quotes in shell script. Basically exploring Bash single vs. double quotes, and what’s the difference?

In shell scripting and other programming languages, single quotes and double quotes are used to define strings. The main difference between single quotes and double quotes is that variables and special characters are not expanded inside single quotes, while they are expanded inside double quotes. When a string is enclosed in single quotes, all the characters within the quotes are treated as a literal string. No variable expansion or command substitution takes place inside single quotes.

Consider this example.
echo ‘My name is $USER’

Output will be this
My name is $USER

In this example, the $USER variable is not expanded inside the single quotes, so the output includes the literal string “$USER”.

In contrast, when a string is enclosed in double quotes, variables and special characters are expanded. Consider this example.

echo “My name is $USER”
Output will be
My name is #CURRENTLY_LOGGED_USERNAME#

In this example, the $USER variable is expanded inside the double quotes to its actual value, which is the name of the current user.

In general, you should use single quotes when you want to treat a string as a literal string without any expansion, and use double quotes when you want to expand variables and special characters within a string.

#linux #ubuntu #redhat #bash