How to print new line character with echo?

The new line character with the echo command is "\n". Taking the following example:

echo -e "This is line 1\nThis is line 2"

Would result in the output

This is line 1
This is line 2

The "-e" parameter is important here.


POSIX 7 says you can't

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

-e is not defined and backslashes are implementation defined:

If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

unless you have an optional XSI extension.

So use printf instead:

format operand shall be used as the format string described in XBD File Format Notation [...]

the File Format Notation:

\n <newline> Move the printing position to the start of the next line.

Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:

  • a Bash built-in: help echo
  • a standalone executable: which echo

which can lead to some confusion.


I finally properly format this string with printf "$string". Thank you all.