How to print new line character with echo?












13















I dump a string with hexdump like this 2031 3334 2e30 0a32 2032 3331 302e 000a. It is clear that 0x0a is new line character, however, when I try to echo this string out, I always got 1 430.2 2 13.0 -- the new line is replaced with a space, even I use the -e flag.



What may be the problem? Does the tailing ruin the output? Is there any alternatives to print 0x0a a new line?



Thanks and Best regards.










share|improve this question

























  • when I try to echo this string out How exactly do you do that? What is the exact command you use?

    – Dennis
    Jul 2 '12 at 19:01











  • @Dennis I mean echo -e. Thanks, I find a solution.

    – Summer_More_More_Tea
    Jul 3 '12 at 2:00











  • the preferred solution is just to use printf "...n" instead of echo.

    – michael
    May 3 '15 at 8:04











  • stackoverflow.com/questions/8467424/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 23 '16 at 23:14
















13















I dump a string with hexdump like this 2031 3334 2e30 0a32 2032 3331 302e 000a. It is clear that 0x0a is new line character, however, when I try to echo this string out, I always got 1 430.2 2 13.0 -- the new line is replaced with a space, even I use the -e flag.



What may be the problem? Does the tailing ruin the output? Is there any alternatives to print 0x0a a new line?



Thanks and Best regards.










share|improve this question

























  • when I try to echo this string out How exactly do you do that? What is the exact command you use?

    – Dennis
    Jul 2 '12 at 19:01











  • @Dennis I mean echo -e. Thanks, I find a solution.

    – Summer_More_More_Tea
    Jul 3 '12 at 2:00











  • the preferred solution is just to use printf "...n" instead of echo.

    – michael
    May 3 '15 at 8:04











  • stackoverflow.com/questions/8467424/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 23 '16 at 23:14














13












13








13


4






I dump a string with hexdump like this 2031 3334 2e30 0a32 2032 3331 302e 000a. It is clear that 0x0a is new line character, however, when I try to echo this string out, I always got 1 430.2 2 13.0 -- the new line is replaced with a space, even I use the -e flag.



What may be the problem? Does the tailing ruin the output? Is there any alternatives to print 0x0a a new line?



Thanks and Best regards.










share|improve this question
















I dump a string with hexdump like this 2031 3334 2e30 0a32 2032 3331 302e 000a. It is clear that 0x0a is new line character, however, when I try to echo this string out, I always got 1 430.2 2 13.0 -- the new line is replaced with a space, even I use the -e flag.



What may be the problem? Does the tailing ruin the output? Is there any alternatives to print 0x0a a new line?



Thanks and Best regards.







bash shell shell-script newlines echo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 3 '12 at 2:12







Summer_More_More_Tea

















asked Jul 2 '12 at 13:27









Summer_More_More_TeaSummer_More_More_Tea

5135927




5135927













  • when I try to echo this string out How exactly do you do that? What is the exact command you use?

    – Dennis
    Jul 2 '12 at 19:01











  • @Dennis I mean echo -e. Thanks, I find a solution.

    – Summer_More_More_Tea
    Jul 3 '12 at 2:00











  • the preferred solution is just to use printf "...n" instead of echo.

    – michael
    May 3 '15 at 8:04











  • stackoverflow.com/questions/8467424/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 23 '16 at 23:14



















  • when I try to echo this string out How exactly do you do that? What is the exact command you use?

    – Dennis
    Jul 2 '12 at 19:01











  • @Dennis I mean echo -e. Thanks, I find a solution.

    – Summer_More_More_Tea
    Jul 3 '12 at 2:00











  • the preferred solution is just to use printf "...n" instead of echo.

    – michael
    May 3 '15 at 8:04











  • stackoverflow.com/questions/8467424/…

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Jan 23 '16 at 23:14

















when I try to echo this string out How exactly do you do that? What is the exact command you use?

– Dennis
Jul 2 '12 at 19:01





when I try to echo this string out How exactly do you do that? What is the exact command you use?

– Dennis
Jul 2 '12 at 19:01













@Dennis I mean echo -e. Thanks, I find a solution.

– Summer_More_More_Tea
Jul 3 '12 at 2:00





@Dennis I mean echo -e. Thanks, I find a solution.

– Summer_More_More_Tea
Jul 3 '12 at 2:00













the preferred solution is just to use printf "...n" instead of echo.

– michael
May 3 '15 at 8:04





the preferred solution is just to use printf "...n" instead of echo.

– michael
May 3 '15 at 8:04













stackoverflow.com/questions/8467424/…

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 23 '16 at 23:14





stackoverflow.com/questions/8467424/…

– Ciro Santilli 新疆改造中心 六四事件 法轮功
Jan 23 '16 at 23:14










3 Answers
3






active

oldest

votes


















23














The new line character with the echo command is "n". Taking the following example:



echo -e "This is line 1nThis is line 2"


Would result in the output



This is line 1
This is line 2


The "-e" parameter is important here.






share|improve this answer



















  • 3





    btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

    – michael
    May 3 '15 at 8:03











  • @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

    – Izzy
    May 4 '15 at 9:28



















3














POSIX 7 says you can't



http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html



-e is not defined and backslashes are implementation defined:




If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.




unless you have an optional XSI extension.



So use printf instead:




format operand shall be used as the format string described in XBD File Format Notation [...]




the File Format Notation:




n <newline> Move the printing position to the start of the next line.




Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:




  • a Bash built-in: help echo

  • a standalone executable: which echo


which can lead to some confusion.






share|improve this answer































    2














    I finally properly format this string with printf "$string". Thank you all.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fsuperuser.com%2fquestions%2f443994%2fhow-to-print-new-line-character-with-echo%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      23














      The new line character with the echo command is "n". Taking the following example:



      echo -e "This is line 1nThis is line 2"


      Would result in the output



      This is line 1
      This is line 2


      The "-e" parameter is important here.






      share|improve this answer



















      • 3





        btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

        – michael
        May 3 '15 at 8:03











      • @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

        – Izzy
        May 4 '15 at 9:28
















      23














      The new line character with the echo command is "n". Taking the following example:



      echo -e "This is line 1nThis is line 2"


      Would result in the output



      This is line 1
      This is line 2


      The "-e" parameter is important here.






      share|improve this answer



















      • 3





        btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

        – michael
        May 3 '15 at 8:03











      • @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

        – Izzy
        May 4 '15 at 9:28














      23












      23








      23







      The new line character with the echo command is "n". Taking the following example:



      echo -e "This is line 1nThis is line 2"


      Would result in the output



      This is line 1
      This is line 2


      The "-e" parameter is important here.






      share|improve this answer













      The new line character with the echo command is "n". Taking the following example:



      echo -e "This is line 1nThis is line 2"


      Would result in the output



      This is line 1
      This is line 2


      The "-e" parameter is important here.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 2 '12 at 13:33









      IzzyIzzy

      2,97521930




      2,97521930








      • 3





        btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

        – michael
        May 3 '15 at 8:03











      • @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

        – Izzy
        May 4 '15 at 9:28














      • 3





        btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

        – michael
        May 3 '15 at 8:03











      • @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

        – Izzy
        May 4 '15 at 9:28








      3




      3





      btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

      – michael
      May 3 '15 at 8:03





      btw, this "echo -e ..." relies on bash's echo to work correctly (e.g., dash will just print the "-e"); but '/bin/echo' may (or may not) also work. Unfortunately (perhaps appropriately), there are a lot of echo's around, and you don't really want to care about which one you're going to get. Using 'printf "....n" is a more portable option across env's and shells.

      – michael
      May 3 '15 at 8:03













      @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

      – Izzy
      May 4 '15 at 9:28





      @michael_n which is what the OP finally reverted too, in fact. But the question was explicitely stating "with echo" – hence my answer this way. Anyhow, it was not "with Bash" – so thanks for your pointing out those differences!

      – Izzy
      May 4 '15 at 9:28













      3














      POSIX 7 says you can't



      http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html



      -e is not defined and backslashes are implementation defined:




      If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.




      unless you have an optional XSI extension.



      So use printf instead:




      format operand shall be used as the format string described in XBD File Format Notation [...]




      the File Format Notation:




      n <newline> Move the printing position to the start of the next line.




      Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:




      • a Bash built-in: help echo

      • a standalone executable: which echo


      which can lead to some confusion.






      share|improve this answer




























        3














        POSIX 7 says you can't



        http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html



        -e is not defined and backslashes are implementation defined:




        If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.




        unless you have an optional XSI extension.



        So use printf instead:




        format operand shall be used as the format string described in XBD File Format Notation [...]




        the File Format Notation:




        n <newline> Move the printing position to the start of the next line.




        Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:




        • a Bash built-in: help echo

        • a standalone executable: which echo


        which can lead to some confusion.






        share|improve this answer


























          3












          3








          3







          POSIX 7 says you can't



          http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html



          -e is not defined and backslashes are implementation defined:




          If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.




          unless you have an optional XSI extension.



          So use printf instead:




          format operand shall be used as the format string described in XBD File Format Notation [...]




          the File Format Notation:




          n <newline> Move the printing position to the start of the next line.




          Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:




          • a Bash built-in: help echo

          • a standalone executable: which echo


          which can lead to some confusion.






          share|improve this answer













          POSIX 7 says you can't



          http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html



          -e is not defined and backslashes are implementation defined:




          If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.




          unless you have an optional XSI extension.



          So use printf instead:




          format operand shall be used as the format string described in XBD File Format Notation [...]




          the File Format Notation:




          n <newline> Move the printing position to the start of the next line.




          Also keep in mind that Ubuntu 15.10 and most distros implement echo both as:




          • a Bash built-in: help echo

          • a standalone executable: which echo


          which can lead to some confusion.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 '16 at 23:11









          Ciro Santilli 新疆改造中心 六四事件 法轮功Ciro Santilli 新疆改造中心 六四事件 法轮功

          4,06622735




          4,06622735























              2














              I finally properly format this string with printf "$string". Thank you all.






              share|improve this answer




























                2














                I finally properly format this string with printf "$string". Thank you all.






                share|improve this answer


























                  2












                  2








                  2







                  I finally properly format this string with printf "$string". Thank you all.






                  share|improve this answer













                  I finally properly format this string with printf "$string". Thank you all.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 3 '12 at 2:02









                  Summer_More_More_TeaSummer_More_More_Tea

                  5135927




                  5135927






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • 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%2fsuperuser.com%2fquestions%2f443994%2fhow-to-print-new-line-character-with-echo%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

                      Probability when a professor distributes a quiz and homework assignment to a class of n students.

                      Aardman Animations

                      Are they similar matrix