Home > Blockchain >  Why does the backslash behavior change depending on the type of quote
Why does the backslash behavior change depending on the type of quote

Time:06-22

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?

CodePudding user response:

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

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

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

  • Related