If bash does not store backslashes in double quotes, then why does echo -e "\ n" work?

From bash manpage

Double-quoted closing characters preserve the literal meaning of all characters within quotation marks, except for $, `, \, and when the story extension is enabled,!.

With that in mind, how does echo -ne "\n" create a new line? Won't the shell expand "\ n" before it is passed to echo ?

I thought this might work because echo is inline, so the shell is smart enough to do the right thing. However, even calling external /usr/bin/echo -ne "\n" works.

What is even more curious is that no matter what I do, double quote or single quote \n , the following two commands show that bash passes \\n as an argument:

 $ strace /usr/bin/echo "\n" 2>&1 | head -n1 execve("/usr/bin/echo", ["/usr/bin/echo", "\\n"], [/* 33 vars */]) = 0 $ strace /usr/bin/echo '\n' 2>&1 | head -n1 execve("/usr/bin/echo", ["/usr/bin/echo", "\\n"], [/* 33 vars */]) = 0 

What's going on here?

+4
source share
1 answer

Read on:

A backslash retains its special meaning only when one of the following characters follows: $ , ` , " , \ or <newline> .

Since it simply follows a letter, it does not retain its special meaning and is transmitted literally to the program.

+4
source

Source: https://habr.com/ru/post/1387982/


All Articles