I just found out that by default backslashes are escaped when using the echo command in a bash shell script.
Check the man page for echo for more info.
Here is an example of how to get a new line using echo (by adding a new line character to a variable)
Example 1
words=”first line \n second line.”
echo $words
produces : first line \n second line.
Example 2 (USING THE -e FLAG)
words=”first line \n second line.”
echo -e $words
produces:
first line
second line
Hi,
this command: echo “first line \nsecond line” will result in the below line:
first line \nsecond line (as you can see there is no new line)
but this one: echo -e “first line \nsecond line” (with the -e) will result in what you want:
echo -e “first line \nsecond line”
first line
second line
Shay
Yep spot on Shay
My original example is probably not as clear as it could have been, I tried to demonstrate that using the echo by it self with the \n does not actually create a new line. Then below that example I include the -e flag and it then creates a new line instead of taking the \n literally.
Thanks for your input, all the best
Tim
thanks – new to bash
It is working
echo -e instead of echo
Simple command but it’s big help for me, thank you.
Very Helpful script but worth noting \r for carriage return for windows pc’s
Regards
Noel
Thanks a lot.. It saved my time & name!!
Yep; me too. Thanks for sharing!
Excellent piece of work. Couldn’t find it anywhere else. Thanks for sharing. Greatly appreciated.
Bingo! Thanks!! Finally got it right
I have a file.I am adding some data into the file.I want each new data to be inserted as a new line into the file.please tell me how to do that??
Hi! So I am using sendEmail -f notifications@domain -t dest@ination,backup-success@domain.com -u “Subject” -m “Body with details `date` HD’s: `echo -e “\n”` `df -h` mem: `free` Loads `uptime` log file `cat /var/log/rsync/log-rsync`” -a /tmp/otherfile -s smtp.gmail.com:587 -o tls=yes -xu notifications@domain -xp pwd
Email does not contain the escaped new line but a simple space. Anyone addressed using `echo -e “\n”` in a similar case?
Thanks!