Why does echo -e “n” give me two blank lines instead of one?
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?
bash text-processing text-formatting
add a comment |
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?
bash text-processing text-formatting
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
add a comment |
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?
bash text-processing text-formatting
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?
bash text-processing text-formatting
bash text-processing text-formatting
asked Feb 28 at 6:34
OhLookOhLook
1275
1275
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
add a comment |
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
1
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
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
add a comment |
1 Answer
1
active
oldest
votes
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 'bbbbn' >>file.txt
Or, another way of doing those two statements with a single redirection:
{
printf 'n'
printf 'bbbbn'
} >>file.txt
Or simply
printf 'nbbbbn' >>file.txt
Related:
- Why is printf better than echo?
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 yourn
, 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 at 6:45
1
@OhLook Why would there be a blank line? Your file would containaaaanbbbbn
where theaaaan
was there from the start and thebbbbn
was added byecho 'bbbb'
. Doingecho -e 'n'
would addnn
to the existing content of the file. If it containsaaaan
already, you'll getaaaannn
.
– Kusalananda
Feb 28 at 6:48
1
@OhLookn
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 at 6:51
1
@OhLookecho -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 at 6:54
|
show 3 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503487%2fwhy-does-echo-e-n-give-me-two-blank-lines-instead-of-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 'bbbbn' >>file.txt
Or, another way of doing those two statements with a single redirection:
{
printf 'n'
printf 'bbbbn'
} >>file.txt
Or simply
printf 'nbbbbn' >>file.txt
Related:
- Why is printf better than echo?
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 yourn
, 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 at 6:45
1
@OhLook Why would there be a blank line? Your file would containaaaanbbbbn
where theaaaan
was there from the start and thebbbbn
was added byecho 'bbbb'
. Doingecho -e 'n'
would addnn
to the existing content of the file. If it containsaaaan
already, you'll getaaaannn
.
– Kusalananda
Feb 28 at 6:48
1
@OhLookn
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 at 6:51
1
@OhLookecho -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 at 6:54
|
show 3 more comments
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 'bbbbn' >>file.txt
Or, another way of doing those two statements with a single redirection:
{
printf 'n'
printf 'bbbbn'
} >>file.txt
Or simply
printf 'nbbbbn' >>file.txt
Related:
- Why is printf better than echo?
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 yourn
, 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 at 6:45
1
@OhLook Why would there be a blank line? Your file would containaaaanbbbbn
where theaaaan
was there from the start and thebbbbn
was added byecho 'bbbb'
. Doingecho -e 'n'
would addnn
to the existing content of the file. If it containsaaaan
already, you'll getaaaannn
.
– Kusalananda
Feb 28 at 6:48
1
@OhLookn
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 at 6:51
1
@OhLookecho -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 at 6:54
|
show 3 more comments
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 'bbbbn' >>file.txt
Or, another way of doing those two statements with a single redirection:
{
printf 'n'
printf 'bbbbn'
} >>file.txt
Or simply
printf 'nbbbbn' >>file.txt
Related:
- Why is printf better than echo?
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 'bbbbn' >>file.txt
Or, another way of doing those two statements with a single redirection:
{
printf 'n'
printf 'bbbbn'
} >>file.txt
Or simply
printf 'nbbbbn' >>file.txt
Related:
- Why is printf better than echo?
edited Feb 28 at 7:32
answered Feb 28 at 6:37
KusalanandaKusalananda
137k17258426
137k17258426
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 yourn
, 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 at 6:45
1
@OhLook Why would there be a blank line? Your file would containaaaanbbbbn
where theaaaan
was there from the start and thebbbbn
was added byecho 'bbbb'
. Doingecho -e 'n'
would addnn
to the existing content of the file. If it containsaaaan
already, you'll getaaaannn
.
– Kusalananda
Feb 28 at 6:48
1
@OhLookn
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 at 6:51
1
@OhLookecho -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 at 6:54
|
show 3 more comments
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 yourn
, 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 at 6:45
1
@OhLook Why would there be a blank line? Your file would containaaaanbbbbn
where theaaaan
was there from the start and thebbbbn
was added byecho 'bbbb'
. Doingecho -e 'n'
would addnn
to the existing content of the file. If it containsaaaan
already, you'll getaaaannn
.
– Kusalananda
Feb 28 at 6:48
1
@OhLookn
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 at 6:51
1
@OhLookecho -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 at 6:54
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
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
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
@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
1
@OhLook Why would there be a blank line? Your file would contain
aaaanbbbbn
where the aaaan
was there from the start and the bbbbn
was added by echo 'bbbb'
. Doing echo -e 'n'
would add nn
to the existing content of the file. If it contains aaaan
already, you'll get aaaannn
.– Kusalananda
Feb 28 at 6:48
@OhLook Why would there be a blank line? Your file would contain
aaaanbbbbn
where the aaaan
was there from the start and the bbbbn
was added by echo 'bbbb'
. Doing echo -e 'n'
would add nn
to the existing content of the file. If it contains aaaan
already, you'll get aaaannn
.– Kusalananda
Feb 28 at 6:48
1
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
@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
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
@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
|
show 3 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503487%2fwhy-does-echo-e-n-give-me-two-blank-lines-instead-of-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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