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,083

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
  • 138,973