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'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 myhostThe 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"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)"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"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'