0

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]']"
[[
]]
user541686
  • 3,033
  • 5
  • 28
  • 43
  • 1
    You need `bash --posix -O xpg_echo -c "echo '[\\n]'"` for the `echo` builtin of `bash` to be fully POSIX compliant. On some systems `xpg_echo` is enabled by default (at least for builds of bash intended for /bin/sh). See [Why is printf better than echo?](https://unix.stackexchange.com/q/65803) for details. – Stéphane Chazelas Dec 23 '22 at 08:44

1 Answers1

5

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?

ilkkachu
  • 133,243
  • 15
  • 236
  • 397