\n not creating new line in bash script

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


first line

second line


12 thoughts on “\n not creating new line in bash script

  1. 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


    • 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

  2. 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??

  3. 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?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s