I am confused on this topic when reading the Advanced Bash Scripting Guide. According to the book,

Bash script, when we quote a string, we set it apart and protect its literal meaning.


Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to interpret that character literally.

Seems that \ is a "functional" character, but what if it's also put in a double quote? Will \ be taken only as a literal or it still does escaping? For example,

echo \z  # z
echo "\z"  # \z  seems that the backslash is taken literally
echo \\  # \
echo "\\"  # \  so why this result is not \\ ?

man bash:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, [...] The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.