LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [Bash] New line character in variable (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bbash%5D-new-line-character-in-variable-454942/)

michael_hk 06-15-2006 01:38 AM

[Bash] New line character in variable
 
Hi,

How can I include a new line character in a variable in a bash shell script?

test.sh
=============
myvar="line1\nline2"
echo $myvar # gives line1\nline2


Doesn't work...and I have already tried many different combinations (\r\n, etc etc).

Thanks in advance.

Michael

druuna 06-15-2006 02:45 AM

Hi,

Use echo -e instead of echo.
Code:

#!/bin/bash
myvar="line1\nline2"
echo -e $myvar

man bash for details.

Hope this helps.

timmeke 06-15-2006 05:38 AM

druuna is right.
See also:
Code:

man echo
-e makes echo interpret some backslash escape sequences like \n, \r, \t, etc.

michael_hk 06-15-2006 06:25 AM

Quote:

Originally Posted by timmeke
druuna is right.
See also:
Code:

man echo
-e makes echo interpret some backslash escape sequences like \n, \r, \t, etc.

Thanks. It works.

spirit receiver 06-15-2006 02:13 PM

No need for the echo command, myvar=$'\n' will also do the trick.


All times are GMT -5. The time now is 01:12 AM.