Skip to main content
2 of 4
added 86 characters in body
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Within double quotes, \\n is an escaped (quoted) backslash followed by a n. This is given to printf as \n and printf will output a newline.

Within double quotes (still), \n is the string \n. Again, printf receives a \n string and will print a newline.

Within double quotes, backslash is special only when preceding another backslash, a newline, or any of $, ` or ". "Special" means that it removes the special meaning of the next character. If a backslash precedes any other character, then it's just a backslash character.

This is explained in the POSIX standard.

To print \n in a printf format string, use printf '\\n' or printf "\\\\n".

Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k