Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new line in the bash string? [duplicate]

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?

like image 784
developer Avatar asked Jul 30 '13 15:07

developer


People also ask

How do I add a new line to a string in bash?

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.

How do you add a line break in shell script?

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.


1 Answers

$ echo "a\nb"
a\nb
$ echo -e "a\nb"
a
b
like image 121
Adam Siemion Avatar answered Oct 02 '22 13:10

Adam Siemion