1

Having simple echo commands:

echo "\n"

and

echo "\\n"

both results in the same output

\n

Can someone please explain me in details how does bash process such a commands (char by char) and why the result is the same in both cases?

  • 2
    Because of bash. Explained here. Your research should have started from man bash, the answer is there. – Kamil Maciorowski Jan 11 '18 at 10:31
  • In those cases it looks like bash and echo are not interpreting \n. (echo needs a special parameter, -e to interpret it). But the \\ is getting interpreted and converted to \ i'm not sure which is interpreting the \\ to \, echo or bash.. But the \\ is interpreted to \ as the \ is escaped to become literal. Hence the same result. Also see re single vs double quotes stackoverflow.com/questions/6697753/… – barlop Jan 11 '18 at 10:33
  • I think based on that SO link that bash would interpret \\ as \ when double quotes are used. Also, you can use a C program to view what a program sees, and therefore see what bash is doing, since what the program receives is already interpreted by bash.This kind of program..I used it in windows,it may need to be adapted slightly for linux superuser.com/questions/328611/… I think the GetCommandLine one is Windows related so ignore that but the code that has printf("argv[%d] = %s\n", i, argv[i]); would be of interest to you. – barlop Jan 11 '18 at 10:41
1

The short answer is that \ is not always special to bash or to echo. Double quotes preserve the literal string value of everything inside except $, `, \, and sometimes !. Outside of double quotes, \ is always special to bash: it preserves the literal string value of the following character. Inside of quotes, the backslash \ is only special when followed by one of five characters: $, `, ", \, or a newline. In the first four cases, it preserves the literal string value of the following character. To the bash version of echo, \ is never special unless the -e flag is provided.

So, in your two cases, what is happening:

echo "\n"

in this case, \ is not followed by one of the five characters mentioned, and so is treated as its literal string value.

echo "\\n"

in this case, the first \ is followed by a second \. The first escapes the second, so that it is interpreted as its literal string value. That is useless here, since it would be interpreted as its literal string value anyway, but it could be useful if you wanted to print a single backslash at the end of a line, like in:

echo "This is a single backslash:\\"

since otherwise the backslash would be interpreted by the shell as escaping ".

All of this information can be gleaned from the part of the bash man page about double quotes, found here.

0

A single \ preserves the literal string value of the character immediately after it.

So in this case:

echo "\\n"

The first \ is preserving the string literal \ that lies before the "n". If you were to type in the following...

echo "\"

...you would need to finish the command since the interpreter would not see the "\" as a string literal but would instead be waiting for user input to escape a "next" character.

However, if the \ is included inside single quotes, then no escape character is waited for as single quotes, by themselves, preserves the literal character(s) of which they enclose.

So for example: echo '\'

This would print the backslash as a string literal and not an escape character so the interpreter will not wait for the user to include a character.

This information, and much more, is included in the bash man page.

  • Your first sentence is not true when the backslash is inside double quotes, and as such does not explain why echo "\n" prints \n instead of n. See my answer. – Chris Jan 14 '18 at 3:32
  • Inside double quotes of an echo statement IS true when the -e argument is provided. echo -e "\$" will print the same as echo -e '$'. Because the backslash preserves the literal string character immediately after it. Meaning my first statement is true. – Yokai Jan 14 '18 at 12:04
  • Inside of a double quote, backslash only preserves the literal string value of $, ", \, and `. – Chris Jan 14 '18 at 20:55

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.