0

This is the contents of my file file.txt:

header
a
b
c

I have no idea what is going on. This command does not print new lines.

echo -e $(tail -n +2 file.txt)

This prints:

a b c

But if you write it to file you will clearly see new lines:

tail -n +2 file.txt >> new_file.txt

test.txt

a
b
c

How do I force echo to print the new lines? I don't think I can use printf here without making some kind of loop.

Dobob
  • 718
  • 1
  • 12
  • 26
  • 2
    Why are you using `echo` here. Just use `tail -n +2 file` – anubhava Jun 20 '17 at 15:33
  • 2
    If you have to use `echo` then quote it: `echo "$(tail -n +2 file)"` or `printf "%s\n" "$(tail -n +2 file)"` – anubhava Jun 20 '17 at 15:34
  • Oh right, I am still learning bash script and was told echo is for printing stuff. But is there a reason why echo doesn't print the new lines? – Dobob Jun 20 '17 at 15:35
  • 2
    `echo` doesn't see them. The shell is removing them, because you used `$(...)` without the quotes. – user1934428 Jun 20 '17 at 15:42
  • BTW, the `-e` argument to `echo` is nonstandard (in fact, any `echo` which does anything but print `-e` on output when given that argument on input is *breaking* [the relevant standard](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), which only allows `-n` to be processed as an option). If you want to process escape sequences, the compliant way is with `printf '%b\n' "your string"`. Even in bash, the noncompliant way can fail if both `posix` and `xpg_echo` flags are set. – Charles Duffy Jun 20 '17 at 15:49
  • Related: [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo) – Charles Duffy Jun 20 '17 at 15:51

1 Answers1

4

Echo statement within quotes give your output with newline terminated lines. Here is the code

echo -e "$(tail -n +2 file.txt)"
Harini
  • 551
  • 5
  • 18