0

I need to output two digits each on a line. The result should be:

1 
2

I tried different approaches and have seen the SO question "Echo newline in Bash prints literal \n".

I do understand this outputs:

bash> echo "1\n2"
1\n2
bash> echo -e "1\n2"
1
2

But what's going on here?

bash> sh -c echo "1\n2"

bash> sh
$ echo "1\n2"
1
2
$ exit
bash> 

My shell is /bin/bash: GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

sh is linked to /bin/dash:

WeSee
  • 3,158
  • 2
  • 30
  • 58
  • 2
    This is because the POSIX spec left this behavior decision up the people implementing it. If you want consistent printing behavior across all mostly POSIX-complaint shells, use `printf` instead. – jordanm May 13 '20 at 17:01
  • 3
    "A string to be written to standard output. If the first operand is -n, or if any of the operands contain a backslash ( '\' ) character, the results are implementation-defined." https://pubs.opengroup.org/onlinepubs/009695399/utilities/echo.html – jordanm May 13 '20 at 17:02

0 Answers0