-1

Let's say there's a line "aaaa" in file.txt, and I want to add ONLY ONE blank line after it, and then add a line of text after the blank line.
I did:
echo -e "\n" >> file.txt
echo "bbbb" >> file.txt
And then I saw TWO blank lines between aaaa and bbbb
When I use only echo "bbbb" >> file.txt then there's no blank line between the two text line.
Why does this happen, and how do I get rid of it?

OhLook
  • 207
  • 1
    OK, let me summarise the correct answer: \n means to move the cursor to the next line and doesn't mean a blank line. – OhLook Feb 28 '19 at 07:30

1 Answers1

5

echo outputs the string that you use as an argument, and then adds a newline character at the end of the outputted string to terminate the line.

With

echo "string"

you get string, and a newline at the end.

Therefore, with

echo -e "\n"

you will get your newline, and a newline at the end (i.e. two empty lines).

If you don't want the extra newline (i.e. to output an unterminated line), use echo with its -n option or, in this case where you just want to insert an empty line, just use echo "" or echo without an argument at all.

From help echo in bash:

Options:
  -n        do not append a newline
  -e        enable interpretation of the following backslash escapes

If you want something that is portable to other shells besides bash, use printf instead:

printf '\n' >>file.txt
printf 'bbbb\n' >>file.txt

Or, another way of doing those two statements with a single redirection:

{
    printf '\n'
    printf 'bbbb\n'
} >>file.txt

Or simply

printf '\nbbbb\n' >>file.txt

Related:

Kusalananda
  • 333,661
  • If there's indeed a newline automatically added to the first line when I used echo, then why did I get no blank line between the text in my second attempt (described under my question)? – OhLook Feb 28 '19 at 06:42
  • 1
    @OhLook In your first attempt, you add two newlines (one is your \n, the other comes from the newline that echo always adds). In your second attempt, you just add the string bbbb with a newline at the end. I don't really see where your confusion comes from. – Kusalananda Feb 28 '19 at 06:45
  • I mean, if there's indeed a newline added to aaaa, then if I do echo "bbbb", there should be a blank line between aaaa and bbbb. But in fact there isn't. – OhLook Feb 28 '19 at 06:46
  • 1
    @OhLook Why would there be a blank line? Your file would contain aaaa\nbbbb\n where the aaaa\n was there from the start and the bbbb\n was added by echo 'bbbb'. Doing echo -e '\n' would add \n\n to the existing content of the file. If it contains aaaa\n already, you'll get aaaa\n\n\n. – Kusalananda Feb 28 '19 at 06:48
  • But there's \n between aaaa and bbbb, why isn't a blank line shown? – OhLook Feb 28 '19 at 06:50
  • 1
    @OhLook \n is a newline character. It moves the cursor to the start of the next line. Had there been no newline at the end of aaaa from the start, you would get aaaabbbb on a single line when appending bbbb. – Kusalananda Feb 28 '19 at 06:51
  • If it just moves the cursor to the start of the next line, why are there two blank lines in my first attempt? – OhLook Feb 28 '19 at 06:52
  • 1
    @OhLook echo -e '\n' adds two newlines. See previous comment. You would have a run of three newline characters in the file. That means, the newline that is the end of the aaaa line (which was there from the start), and then two added by echo -e '\n'. This makes for two empty lines. – Kusalananda Feb 28 '19 at 06:54