The new line \n
is not taken account in the shell strings:
str="aaa\nbbbb"
echo $str
Output:
aaa\nbbbb
Expected result:
aaa
bbbb
How can I add a new line in the string?
Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.
The most used newline character If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line. An example is below.
$ echo "a\nb"
a\nb
$ echo -e "a\nb"
a
b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With