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?

  • 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 at 7:30

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.


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:

  -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


  • 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 at 6: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 at 6:45
  • 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 at 6:48
  • 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 at 6:51
  • 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 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.