Closed nijel closed 6 years ago
I'm not entirely sure that they shouldn't. When you use \\\n
logically what you have is escaped \ plus meaningless \n triggering fallback behavior. That could cause issues while passing it down. Like I said, I assume the correct form is probably \\\\n
.
While I agree with you, the sh implementation does not:
$ sh -c 'echo "\n"'
$ bash -c 'echo "\n"'
\n
$ sh -c 'echo "\\n"'
$ bash -c 'echo "\\n"'
\n
$ sh -c 'echo "\\\n"'
\n
$ bash -c 'echo "\\\n"'
\\n
$ sh -c 'echo "\\\\n"'
\n
$ bash -c 'echo "\\\\n"'
\\n
Afaik know in our Debian the sh implementation is provided by Dash (by default), which does indeed treat backslashes differently in echo.
$ dash -c 'echo "\\\\n"'
\n
That doesn't answer the question though. The same behavior shouldn't occur when not using echo. For example, when using the actually portable printf
.
$ dash -c 'bla="\\\\n"; printf "%s" "$bla"'
\\n
Also use unicode escape sequence for newline to avoid problems with some shells decoding \n even when they probably should not.