Monday, July 16, 2012

echo-ing new lines in bash

For the sake of our memories:
$ echo "line\nnewline"
line\nnewline
$ echo -e "line\nnewline"
line
newline
$ lines=`echo -e "line\nnewline"`
$ echo $lines
line newline
$ echo "$lines"
line
newline
Clearly you must use -e if you want echo to recognize newline escape character (\n). In addition notice how quotes are needed to print the new line stored in a variable while the -e option is not needed.

No comments:

Followers