If you’re using the Bash shell:
$ echo $0
bash
You may notice that newline characters are escaped in strings:
$ echo "hello\nworld"
hello\nworld
echo
To preserve the newline in echo
, you can set option -e
:
$ echo -e 'hello\nworld'
hello
world
variable
Alternatively, you can assign the newline character to a variable:
$ NL=$'\n'
And then substitute it via parameter expansion:
$ echo "hello${NL}world"
hello
world
Make sure to use double quotes "..."
or else your variable reference won’t be expanded.