Why does echo -e “n” give me two blank lines instead of one?












-2















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?










share|improve this question


















  • 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
















-2















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?










share|improve this question


















  • 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














-2












-2








-2








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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










1 Answer
1






active

oldest

votes


















3














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?






share|improve this answer


























  • 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 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





    @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








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
});


}
});














draft saved

draft discarded


















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









3














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?






share|improve this answer


























  • 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 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





    @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


















3














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?






share|improve this answer


























  • 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 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





    @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
















3












3








3







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?






share|improve this answer















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?







share|improve this answer














share|improve this answer



share|improve this answer








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 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 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





    @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





















  • 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 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





    @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



















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀