[SOLVED] Why does the backslash behavior change depending on the type of quote


I’m very new to bash and came across this example where the output is different depending on the quote type.

Here’s the first example:

echo -e "\\\\"

This prints out one backslash character.
Whereas the following:

echo -e '\\\\'

Prints two backslash characters.
Does anyone know why?


In double quotes, some characters have special meaning, and a backslash is used to "escape" the special meaning, i.e. insert them literally.

"$x"   # Value of the variable $x.
"\$x"  # The string $x.

To enter a literal backslash to double quotes, you need to escape it by another backslash, so backslash in double quotes is represented as \\.

In single quotes, every character is interpreted literally, so there’s no need for an escape character.

Answered By – choroba

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.