-1

ANSI C Quoting in bash is supposed to read strings the same way C(or python with single quotes) would.

printf $'"Hello, World!\\n"'

#expected output (no newline, the slash is escaped)
"Hello, World!\n"

#actual output (one newline)
"Hello, World!
"

But it seems to not be working properly. Is that a bug?

1 Answers1

3

You're using printf wrong. You have provided the string where a format string is expected, and printf interprets \n accordingly. Use a proper format string and provide the ANSI-C-quoted string as an argument:

$ printf '%s\n' $'"Hello, World!\\n"'
"Hello, World!\n"

Or, to show more clearly where the argument goes in the format string:

$ printf '|%s|\n' $'"Hello, World!\\n"'
|"Hello, World!\n"|
muru
  • 72,889