This question is related to How do I print backslash followed by newline with printf?, where OP tries to print \\\n
as single backslash followed by newline (not literal \n
).
While by shell rules it makes sense that \\
would be expanded as \
and \n
as n
(i.e., shell performs backslash escape to preserve the literal form of the following character), when I perform strace
it appears as if shell performs something entirely different and I'm struggling to interpret what I'm seeing.
$ strace -e execve printf "\\\n"
execve("/usr/bin/printf", ["printf", "\\\\n"], [/* 42 vars */]) = 0
\n+++ exited with 0 +++
In other words, what I am seeing is that instead of reduced number of characters that goes into argv
portion of execve
syscall, the number actually increases and there's appended an extra backslash.
Passing single quoted '\\\\n'
is even more confusing:
$ strace -e execve printf '\\\\n'
execve("/usr/bin/printf", ["printf", "\\\\\\\\n"], [/* 42 vars */]) = 0
\\n+++ exited with 0 +++
In other words, I'd expect that here shell would pass everything unaltered to printf
just as in execve()
from previous command and get same output as printf "\\\n"
, but it's different.
To some extent I seem to circle around understanding that pure printf
itself ( the one executed by system) would interpret the argument \\\n
placed in argv
portion of execve()
as backslash and newline. Meanwhile, shell would need to convert \\\n
typed in by user to match its own rules, but I am struggling to verbalize what exactly is happening with multiple backslashes.