Aren't single quotes supposed to be "hard" quotes in POSIX shells?
Why does dash seem to process escapes inside them, unlike Bash?
$ bash --posix -c "echo ['[\\n]']"
[[\n]]
$ dash -c "echo ['[\\n]']"
[[
]]
Aren't single quotes supposed to be "hard" quotes in POSIX shells?
Why does dash seem to process escapes inside them, unlike Bash?
$ bash --posix -c "echo ['[\\n]']"
[[\n]]
$ dash -c "echo ['[\\n]']"
[[
]]
The single quotes do behave identically, it's the echo
implementation that's different.
When you run sh -c "echo ['[\\n]']"
(for any sh
), the first thing that happens is that your interactive shell processes the double-quoted string, turning the \\
into a single \
. The string that gets passed to the shell for execution is then echo ['[\n]']
.
The echo
command (probably builtin) launched within that inner shell sees the single argument [[\n]]
, and some implementations of echo
process C-style backslash-escapes like \n
for newline, while some others don't.
Try sh -c "printf '%s\\n' ['[\\n]']"
instead, and see Why is printf better than echo?
bash --posix -O xpg_echo -c "echo '[\\n]'"
for theecho
builtin ofbash
to be fully POSIX compliant. On some systemsxpg_echo
is enabled by default (at least for builds of bash intended for /bin/sh). See Why is printf better than echo? for details. – Stéphane Chazelas Dec 23 '22 at 08:44