Quotations in Bash can be tricky, combining single quotes ' and double quotes " can result in unexpected behaviours; and escaping those quotes can quickly become very confusing.

Leading double quotes

Single quotes within the double quotes as the leading string encapsulation have no special meaning beyond the ' character itself. Hence, the variable substitution $result and any process substitution $(…) is interpreted as such and replaced in the output.

result=42; echo "The quoted result is '$result'"
: [➥] The quoted result is '42'
result=42; echo "The quoted result is ''$result''"
: [➥] The quoted result is ''42''
echo "The quoted hostname is '`hostname`'"
: [➥] The quoted hostname is 'myhost'
echo "The quoted hostname is '$(hostname)'"
: [➥] The quoted hostname is 'myhost'
Single quotes within leading double quotes

Double quotes within leading double quotes is already a bit tricky

result=42; echo "The quoted result is "$result""
: [➥] The quoted result is 42
echo "The quoted hostname is "$(hostname)""
: [➥] The quoted hostname is myhost
Doubles quotes within double quotes

The second time a double quote is found, it closes the first ones opening the string. So the previous example is interpreted as string + substitution + empty string.

Printing a " character with leading double quotes requires to either use single quotes or escape characters (i.e. \").

# WRONG: string + $result + empty string ("")
result=42; echo "The quoted result is "$result""
: [➥] The quoted result is 42
# CORRECT: string + quoted double quote ('"') + variable + ('"')
result=42; echo "The quoted result is "'"'$result'"'
: [➥] The quoted result is "42"
# CORRECT: string with escaped double quotes
result=42; echo "The quoted result is \"$result\""
: [➥] The quoted result is "42"
Solution to doubles quotes within double quotes

Leading single quotes

Single quotes have the special meaning of treating all encapsulated characters as literals. Including the escape characters. Hence, it is trivial to print double quotes within single quotes, but no substitution will occur.

result=42; echo 'The quoted result is "$result"'
: [➥] The quoted result is "$result"
result=42; echo 'The quoted result is \"$result\"'
: [➥] The quoted result is \"$result\"
echo 'The quoted hostname is "$(hostname)"'
: [➥] The quoted hostname is "$(hostname)"
Double quotes within leading single quotes

In this case, the solution is to close the single quotes before the substitution.

# string (including " quotes) + variable + string (including " quotes)
result=42; echo 'The quoted result is "'$result'"'
: [➥] The quoted result is "42"
Solution to double quotes within leading single quotes

Printing a ' character with leading single quotes cannot be done, the second ' will always be interpreted as the closing single quote. Even if it is escaped, as the escape character is a literal backslash. Therefore, the solution requires closing the single quotes as well and printing the single quote within another context.

# WRONG: string + variable + empty string
result=42; echo 'The quoted result is '$result''
: [➥] The quoted result is 42
# WRONG: string (including a backslash) + variable + empty string
result=42; echo 'The quoted result is \'$result''
: [➥] The quoted result is \42
# CORRECT: single quoted string + double quoted string (including ' quotes)
result=42; echo 'The quoted result is '"'$result'"
: [➥] The quoted result is '42'
Solution to single quotes within leading single quotes