6

\n is not recognized as "change line". It just prints \n.

My terminal is gnome terminal 3.6.2.

First I noticed it with echo commands,and then with shell scripts.

Any suggestions as to why it doesn't work?

muru
  • 193,181
  • 53
  • 473
  • 722
PaschalisAv
  • 143
  • 1
  • 1
  • 6

2 Answers2

10

By default, the standard GNU version of echo delivered with Ubuntu, doesn't recognize escape sequences. Use the -e flag to enable that.

Compare the outputs:

serg@ubuntu(bash):[/home/xieerqi]$ echo "new\nline"
new\nline
serg@ubuntu(bash):[/home/xieerqi]$ echo -e "new\nline"
new
line

In general echo in scripts isn't recommended. For instance, mksh version of echo does allow interpreting the escapes.

For portability of scripts, use the printf function.

serg@ubuntu(bash):[/home/xieerqi]$ printf "new\nline\n"
new
line
Fabby
  • 34,341
  • 38
  • 97
  • 191
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • Yes, you are right. It works with printf and I had tried that. And then I wrote this line just to try it with other commands: – PaschalisAv Aug 07 '15 at 06:57
  • date '+DATE:%m-%y \n TIME:%H:%M:%S' and again it didn't work. I am just puzzled. I don't know with which command it works and with which it doesn't. Anyway. Thank you very much! – PaschalisAv Aug 07 '15 at 06:59
  • @user1622666 So as you see, `printf` is better than echo, so get into habit of using that. While echo behaves differently from OS to OS, printf works across the board – Sergiy Kolodyazhnyy Aug 07 '15 at 07:00
  • @user1622666 `date` won't interpret \n. Just separate the `date` command in two. Like so `date '+DATE:%m-%y';date '+TIME:%H:%M:%S' ` – Sergiy Kolodyazhnyy Aug 07 '15 at 07:03
  • @user1622666 by the way, if you like my answer, please leave an upvote and if my answer solved your question, please use the checkmark on the left to accept this answer as the correct solution. Thank you in advance ! – Sergiy Kolodyazhnyy Aug 07 '15 at 07:04
  • Oh, I didn't knew it. I could swear that I have used it before with date. Yes, separating it is a very good idea. Thank you! – PaschalisAv Aug 07 '15 at 07:07
  • @user1622666 just out of curriosity I replaced backlash with % , so this worked: `date "+DATE:%m-%y %n TIME:%H:%M:%S" ` . Somewhat unexpected , but works – Sergiy Kolodyazhnyy Aug 07 '15 at 07:13
  • 2
    @user1622666: As a general rule, most commands do NOT understand \n any more than they understand HTML. Each command's manual page will tell you what processing it does on its input. – Stig Hemmer Aug 07 '15 at 09:47
2

The answer by Serg solves only the problem with printing newline (echo, printf). If you need to use newline in general in shell scripts here are some suggestions which demostrate use of a newline by storing it to a variable $NL. When the code works correctly echo "a${NL}b" then prints:

a
b

POSIX compliant sh

You can use a literal newline:

NL="
"

You can generate the newline using printf and command substitution:

NLx="$(printf \\nx)" ; NL="${NLx%x}"

The appending of x and the following removal of it is necessary because the command substitution removes all newlines at the end of the resulting string.

Bash

In Bash you can use escape sequences to represent control characters in a string between $' and ':

NL=$'\n'

This is very convenient but unfortunately not portable.