Echo newline in Bash prints literal

ID : 201

viewed : 96

Tags : bashechonewlinebash

Top 5 Answer for Echo newline in Bash prints literal

vote vote

96

You could use printf instead:

printf "hello\nworld\n" 

printf has more consistent behavior than echo. The behavior of echo varies greatly between different versions.

vote vote

86

Make sure you are in Bash. All these four ways work for me:

echo -e "Hello\nworld" echo -e 'Hello\nworld' echo Hello$'\n'world echo Hello ; echo world 
vote vote

79

echo $'hello\nworld' 

prints

hello world 

$'' strings use 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.

vote vote

66

You could always do echo "".

For example,

echo "Hello," echo "" echo "World!" 
vote vote

58

In the off chance that someone finds themselves beating their head against the wall trying to figure out why a coworker's script won't print newlines, look out for this:

#!/bin/bash function GET_RECORDS() {    echo -e "starting\n the process"; }  echo $(GET_RECORDS); 

As in the above, the actual running of the method may itself be wrapped in an echo which supersedes any echos that may be in the method itself. Obviously I watered this down for brevity. It was not so easy to spot!

You can then inform your comrades that a better way to execute functions would be like so:

#!/bin/bash function GET_RECORDS() {    echo -e "starting\n the process"; }  GET_RECORDS; 

Top 3 video Explaining Echo newline in Bash prints literal

Related QUESTION?