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?
Asked
Active
Viewed 1.1k times
-1

OhLook
- 207
-
1OK, 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 Answers
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 thatecho
always adds). In your second attempt, you just add the stringbbbb
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 theaaaa\n
was there from the start and thebbbb\n
was added byecho 'bbbb'
. Doingecho -e '\n'
would add\n\n
to the existing content of the file. If it containsaaaa\n
already, you'll getaaaa\n\n\n
. – Kusalananda Feb 28 '19 at 06: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 ofaaaa
from the start, you would getaaaabbbb
on a single line when appendingbbbb
. – 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 theaaaa
line (which was there from the start), and then two added byecho -e '\n'
. This makes for two empty lines. – Kusalananda Feb 28 '19 at 06:54