-2

Today I encountered this:

$ echo $'foo\\n'
foo\n
$ echo 'foo\\n'
foo\\n

I have never come across this before and never heard anyone mention this. I would've expected $ outside of strings to cause that string to be treated as a variable name. Where is this documented and has it always been this way?

1
  • 2
    Tip: To find this in your system's built-in bash(1) man page, type man bash and then type /\$\' to search for the literal string $'.
    – Spiff
    Dec 24, 2020 at 2:59

1 Answer 1

2

This is bash's ANSI-C Quoting

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

It's documented here https://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting

You must log in to answer this question.