Why is echo “bar” -n different from echo -n “bar”?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







10















Compare



echo -n "bar"


with



echo "bar" -n


The former does what I think it's supposed to do (prints "bar" without a newline), while the latter doesn't. Is this a bug or by design? Why is it different than many cli programs in that you can't move the options around? For example, tail -f /var/log/messages is exactly the same as tail /var/log/messages -f. I sometimes do the latter when I forget I wanted the former, because internally the options and arguments are rearranged, usually by getopt.



Update: yes I originally nerf-ed my question. I removed the nerf you'll have to view history to make some of the answers make sense.










share|improve this question




















  • 3





    From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

    – phunehehe
    Jan 20 '11 at 5:42


















10















Compare



echo -n "bar"


with



echo "bar" -n


The former does what I think it's supposed to do (prints "bar" without a newline), while the latter doesn't. Is this a bug or by design? Why is it different than many cli programs in that you can't move the options around? For example, tail -f /var/log/messages is exactly the same as tail /var/log/messages -f. I sometimes do the latter when I forget I wanted the former, because internally the options and arguments are rearranged, usually by getopt.



Update: yes I originally nerf-ed my question. I removed the nerf you'll have to view history to make some of the answers make sense.










share|improve this question




















  • 3





    From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

    – phunehehe
    Jan 20 '11 at 5:42














10












10








10


2






Compare



echo -n "bar"


with



echo "bar" -n


The former does what I think it's supposed to do (prints "bar" without a newline), while the latter doesn't. Is this a bug or by design? Why is it different than many cli programs in that you can't move the options around? For example, tail -f /var/log/messages is exactly the same as tail /var/log/messages -f. I sometimes do the latter when I forget I wanted the former, because internally the options and arguments are rearranged, usually by getopt.



Update: yes I originally nerf-ed my question. I removed the nerf you'll have to view history to make some of the answers make sense.










share|improve this question
















Compare



echo -n "bar"


with



echo "bar" -n


The former does what I think it's supposed to do (prints "bar" without a newline), while the latter doesn't. Is this a bug or by design? Why is it different than many cli programs in that you can't move the options around? For example, tail -f /var/log/messages is exactly the same as tail /var/log/messages -f. I sometimes do the latter when I forget I wanted the former, because internally the options and arguments are rearranged, usually by getopt.



Update: yes I originally nerf-ed my question. I removed the nerf you'll have to view history to make some of the answers make sense.







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 '11 at 18:19









Steven D

32.9k898108




32.9k898108










asked Jan 20 '11 at 3:16









xenoterracidexenoterracide

26.1k53159222




26.1k53159222








  • 3





    From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

    – phunehehe
    Jan 20 '11 at 5:42














  • 3





    From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

    – phunehehe
    Jan 20 '11 at 5:42








3




3





From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

– phunehehe
Jan 20 '11 at 5:42





From what I see, the difference is immediately obvious. echo -n "bar" gives "bar", while echo "bar" -n gives "bar -n"

– phunehehe
Jan 20 '11 at 5:42










4 Answers
4






active

oldest

votes


















18














As most others have observed, "-n" is interpreted literally if placed anywhere but immediately after the echo command.



Historically, UNIX utilities were all like this -- they looked for options only immediately after the command name. It was likely either BSD or GNU who pioneered the more flexible style (though I could be wrong), as even now POSIX specifies the old way as correct (see Guideline 9, and also man 3 getopt on a Linux system). Anyway, even though most Linux utilities these days use the new style, there are some holdouts like echo.



Echo is a mess, standards-wise, in that there were at least two fundamentally conflicting versions in play by the time POSIX came into being. On the one hand, you have SYSV-style, which interprets backslash-escaped characters but otherwise treats its arguments literally, accepting no options. On the other, you have BSD-style, which treats an initial -n as a special case and outputs absolutely everything else literally. And since echo is so convenient, you have thousands of shell scripts that depend on one behavior or the other:



echo Usage: my_awesome_script '[-a]' '[-b]' '[-c]' '[-n]'
echo -a does a thing.
echo -b does something else.
echo -c makes sure -a works right.
echo -- DON'T USE -n -- it's not finished! --


Because of the "treat everything literally" semantic, it's impossible to even add a new option to echo without breaking things. If GNU used the flexible options scheme on it, hell would break loose.



Incidentally, for best compatibility between Bourne shell implementations, use printf rather than echo.



UPDATED to explain why echo in particular does not use flexible options.






share|improve this answer


























  • upvote since you're the only one to mention that's the expected getopt behavior.

    – Geoffrey Bachelet
    Jan 20 '11 at 8:41











  • @jander why is echo a "holdout"? I think it's a gnu coreutil?

    – xenoterracide
    Jan 20 '11 at 10:25











  • @xeno: I've updated my answer. Basically, GNU didn't want to break things.

    – Jander
    Jan 20 '11 at 16:16






  • 1





    Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

    – Gilles
    Jan 20 '11 at 21:29






  • 1





    For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

    – Gilles
    Jan 20 '11 at 21:30



















9














The other answers get to the point that you can look at the man page to see that -n is becoming part of the string to echo. However I just want to point out that it's easy to investigate this without the md5sum and makes what's going on a little less cryptic.



[11:07:44][dasonk@Chloe:~]: echo -n "bar"
bar[11:07:48][dasonk@Chloe:~]: echo "bar" -n
bar -n





share|improve this answer



















  • 3





    +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

    – Mikel
    Jan 20 '11 at 5:20



















6














Wow, everyone's explanations are lengthy.



It's this simple:



-n is an option if it appears before the string, otherwise it's just another string to be echoed.



Remove the | md5sum and you'll see the output is different.






share|improve this answer































    5














    From simple inspection, this is not a bug by design...



    echo "bar" -n | md5sum

    a3f8efa5dd10e90aee0963052e3650a1


    Try this command for yourself:



    echo "bar -n" | md5sum


    You will notice that the resulting md5 is



    a3f8efa5dd10e90aee0963052e3650a1


    You just mixed-up the use of -n in echo.



    In your first sample code



    echo -n "bar" | md5sum


    -n is used to say DO NOT to put a newline after this echo.



    Your second sample



    echo "bar" -n | md5sum


    is treating -n as a literal text.






    share|improve this answer


























      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%2f6162%2fwhy-is-echo-bar-n-different-from-echo-n-bar%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      18














      As most others have observed, "-n" is interpreted literally if placed anywhere but immediately after the echo command.



      Historically, UNIX utilities were all like this -- they looked for options only immediately after the command name. It was likely either BSD or GNU who pioneered the more flexible style (though I could be wrong), as even now POSIX specifies the old way as correct (see Guideline 9, and also man 3 getopt on a Linux system). Anyway, even though most Linux utilities these days use the new style, there are some holdouts like echo.



      Echo is a mess, standards-wise, in that there were at least two fundamentally conflicting versions in play by the time POSIX came into being. On the one hand, you have SYSV-style, which interprets backslash-escaped characters but otherwise treats its arguments literally, accepting no options. On the other, you have BSD-style, which treats an initial -n as a special case and outputs absolutely everything else literally. And since echo is so convenient, you have thousands of shell scripts that depend on one behavior or the other:



      echo Usage: my_awesome_script '[-a]' '[-b]' '[-c]' '[-n]'
      echo -a does a thing.
      echo -b does something else.
      echo -c makes sure -a works right.
      echo -- DON'T USE -n -- it's not finished! --


      Because of the "treat everything literally" semantic, it's impossible to even add a new option to echo without breaking things. If GNU used the flexible options scheme on it, hell would break loose.



      Incidentally, for best compatibility between Bourne shell implementations, use printf rather than echo.



      UPDATED to explain why echo in particular does not use flexible options.






      share|improve this answer


























      • upvote since you're the only one to mention that's the expected getopt behavior.

        – Geoffrey Bachelet
        Jan 20 '11 at 8:41











      • @jander why is echo a "holdout"? I think it's a gnu coreutil?

        – xenoterracide
        Jan 20 '11 at 10:25











      • @xeno: I've updated my answer. Basically, GNU didn't want to break things.

        – Jander
        Jan 20 '11 at 16:16






      • 1





        Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

        – Gilles
        Jan 20 '11 at 21:29






      • 1





        For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

        – Gilles
        Jan 20 '11 at 21:30
















      18














      As most others have observed, "-n" is interpreted literally if placed anywhere but immediately after the echo command.



      Historically, UNIX utilities were all like this -- they looked for options only immediately after the command name. It was likely either BSD or GNU who pioneered the more flexible style (though I could be wrong), as even now POSIX specifies the old way as correct (see Guideline 9, and also man 3 getopt on a Linux system). Anyway, even though most Linux utilities these days use the new style, there are some holdouts like echo.



      Echo is a mess, standards-wise, in that there were at least two fundamentally conflicting versions in play by the time POSIX came into being. On the one hand, you have SYSV-style, which interprets backslash-escaped characters but otherwise treats its arguments literally, accepting no options. On the other, you have BSD-style, which treats an initial -n as a special case and outputs absolutely everything else literally. And since echo is so convenient, you have thousands of shell scripts that depend on one behavior or the other:



      echo Usage: my_awesome_script '[-a]' '[-b]' '[-c]' '[-n]'
      echo -a does a thing.
      echo -b does something else.
      echo -c makes sure -a works right.
      echo -- DON'T USE -n -- it's not finished! --


      Because of the "treat everything literally" semantic, it's impossible to even add a new option to echo without breaking things. If GNU used the flexible options scheme on it, hell would break loose.



      Incidentally, for best compatibility between Bourne shell implementations, use printf rather than echo.



      UPDATED to explain why echo in particular does not use flexible options.






      share|improve this answer


























      • upvote since you're the only one to mention that's the expected getopt behavior.

        – Geoffrey Bachelet
        Jan 20 '11 at 8:41











      • @jander why is echo a "holdout"? I think it's a gnu coreutil?

        – xenoterracide
        Jan 20 '11 at 10:25











      • @xeno: I've updated my answer. Basically, GNU didn't want to break things.

        – Jander
        Jan 20 '11 at 16:16






      • 1





        Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

        – Gilles
        Jan 20 '11 at 21:29






      • 1





        For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

        – Gilles
        Jan 20 '11 at 21:30














      18












      18








      18







      As most others have observed, "-n" is interpreted literally if placed anywhere but immediately after the echo command.



      Historically, UNIX utilities were all like this -- they looked for options only immediately after the command name. It was likely either BSD or GNU who pioneered the more flexible style (though I could be wrong), as even now POSIX specifies the old way as correct (see Guideline 9, and also man 3 getopt on a Linux system). Anyway, even though most Linux utilities these days use the new style, there are some holdouts like echo.



      Echo is a mess, standards-wise, in that there were at least two fundamentally conflicting versions in play by the time POSIX came into being. On the one hand, you have SYSV-style, which interprets backslash-escaped characters but otherwise treats its arguments literally, accepting no options. On the other, you have BSD-style, which treats an initial -n as a special case and outputs absolutely everything else literally. And since echo is so convenient, you have thousands of shell scripts that depend on one behavior or the other:



      echo Usage: my_awesome_script '[-a]' '[-b]' '[-c]' '[-n]'
      echo -a does a thing.
      echo -b does something else.
      echo -c makes sure -a works right.
      echo -- DON'T USE -n -- it's not finished! --


      Because of the "treat everything literally" semantic, it's impossible to even add a new option to echo without breaking things. If GNU used the flexible options scheme on it, hell would break loose.



      Incidentally, for best compatibility between Bourne shell implementations, use printf rather than echo.



      UPDATED to explain why echo in particular does not use flexible options.






      share|improve this answer















      As most others have observed, "-n" is interpreted literally if placed anywhere but immediately after the echo command.



      Historically, UNIX utilities were all like this -- they looked for options only immediately after the command name. It was likely either BSD or GNU who pioneered the more flexible style (though I could be wrong), as even now POSIX specifies the old way as correct (see Guideline 9, and also man 3 getopt on a Linux system). Anyway, even though most Linux utilities these days use the new style, there are some holdouts like echo.



      Echo is a mess, standards-wise, in that there were at least two fundamentally conflicting versions in play by the time POSIX came into being. On the one hand, you have SYSV-style, which interprets backslash-escaped characters but otherwise treats its arguments literally, accepting no options. On the other, you have BSD-style, which treats an initial -n as a special case and outputs absolutely everything else literally. And since echo is so convenient, you have thousands of shell scripts that depend on one behavior or the other:



      echo Usage: my_awesome_script '[-a]' '[-b]' '[-c]' '[-n]'
      echo -a does a thing.
      echo -b does something else.
      echo -c makes sure -a works right.
      echo -- DON'T USE -n -- it's not finished! --


      Because of the "treat everything literally" semantic, it's impossible to even add a new option to echo without breaking things. If GNU used the flexible options scheme on it, hell would break loose.



      Incidentally, for best compatibility between Bourne shell implementations, use printf rather than echo.



      UPDATED to explain why echo in particular does not use flexible options.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 20 '11 at 16:15

























      answered Jan 20 '11 at 7:56









      JanderJander

      11.9k43358




      11.9k43358













      • upvote since you're the only one to mention that's the expected getopt behavior.

        – Geoffrey Bachelet
        Jan 20 '11 at 8:41











      • @jander why is echo a "holdout"? I think it's a gnu coreutil?

        – xenoterracide
        Jan 20 '11 at 10:25











      • @xeno: I've updated my answer. Basically, GNU didn't want to break things.

        – Jander
        Jan 20 '11 at 16:16






      • 1





        Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

        – Gilles
        Jan 20 '11 at 21:29






      • 1





        For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

        – Gilles
        Jan 20 '11 at 21:30



















      • upvote since you're the only one to mention that's the expected getopt behavior.

        – Geoffrey Bachelet
        Jan 20 '11 at 8:41











      • @jander why is echo a "holdout"? I think it's a gnu coreutil?

        – xenoterracide
        Jan 20 '11 at 10:25











      • @xeno: I've updated my answer. Basically, GNU didn't want to break things.

        – Jander
        Jan 20 '11 at 16:16






      • 1





        Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

        – Gilles
        Jan 20 '11 at 21:29






      • 1





        For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

        – Gilles
        Jan 20 '11 at 21:30

















      upvote since you're the only one to mention that's the expected getopt behavior.

      – Geoffrey Bachelet
      Jan 20 '11 at 8:41





      upvote since you're the only one to mention that's the expected getopt behavior.

      – Geoffrey Bachelet
      Jan 20 '11 at 8:41













      @jander why is echo a "holdout"? I think it's a gnu coreutil?

      – xenoterracide
      Jan 20 '11 at 10:25





      @jander why is echo a "holdout"? I think it's a gnu coreutil?

      – xenoterracide
      Jan 20 '11 at 10:25













      @xeno: I've updated my answer. Basically, GNU didn't want to break things.

      – Jander
      Jan 20 '11 at 16:16





      @xeno: I've updated my answer. Basically, GNU didn't want to break things.

      – Jander
      Jan 20 '11 at 16:16




      1




      1





      Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

      – Gilles
      Jan 20 '11 at 21:29





      Accepting options after the first non-option is specific to GNU utilities and program using GNU libc's getopt facilities. The user can always get backward-compatible behavior by setting the environment variable POSIXLY_CORRECT.

      – Gilles
      Jan 20 '11 at 21:29




      1




      1





      For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

      – Gilles
      Jan 20 '11 at 21:30





      For the specific case of echo, there are also implementations that recognize -e or -E as options. For portability, don't start with a - or use something like printf %s -e.

      – Gilles
      Jan 20 '11 at 21:30













      9














      The other answers get to the point that you can look at the man page to see that -n is becoming part of the string to echo. However I just want to point out that it's easy to investigate this without the md5sum and makes what's going on a little less cryptic.



      [11:07:44][dasonk@Chloe:~]: echo -n "bar"
      bar[11:07:48][dasonk@Chloe:~]: echo "bar" -n
      bar -n





      share|improve this answer



















      • 3





        +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

        – Mikel
        Jan 20 '11 at 5:20
















      9














      The other answers get to the point that you can look at the man page to see that -n is becoming part of the string to echo. However I just want to point out that it's easy to investigate this without the md5sum and makes what's going on a little less cryptic.



      [11:07:44][dasonk@Chloe:~]: echo -n "bar"
      bar[11:07:48][dasonk@Chloe:~]: echo "bar" -n
      bar -n





      share|improve this answer



















      • 3





        +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

        – Mikel
        Jan 20 '11 at 5:20














      9












      9








      9







      The other answers get to the point that you can look at the man page to see that -n is becoming part of the string to echo. However I just want to point out that it's easy to investigate this without the md5sum and makes what's going on a little less cryptic.



      [11:07:44][dasonk@Chloe:~]: echo -n "bar"
      bar[11:07:48][dasonk@Chloe:~]: echo "bar" -n
      bar -n





      share|improve this answer













      The other answers get to the point that you can look at the man page to see that -n is becoming part of the string to echo. However I just want to point out that it's easy to investigate this without the md5sum and makes what's going on a little less cryptic.



      [11:07:44][dasonk@Chloe:~]: echo -n "bar"
      bar[11:07:48][dasonk@Chloe:~]: echo "bar" -n
      bar -n






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 20 '11 at 5:09









      DasonDason

      21349




      21349








      • 3





        +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

        – Mikel
        Jan 20 '11 at 5:20














      • 3





        +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

        – Mikel
        Jan 20 '11 at 5:20








      3




      3





      +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

      – Mikel
      Jan 20 '11 at 5:20





      +1 exactly. If a pipeline isn't doing what you expect, test each part separately.

      – Mikel
      Jan 20 '11 at 5:20











      6














      Wow, everyone's explanations are lengthy.



      It's this simple:



      -n is an option if it appears before the string, otherwise it's just another string to be echoed.



      Remove the | md5sum and you'll see the output is different.






      share|improve this answer




























        6














        Wow, everyone's explanations are lengthy.



        It's this simple:



        -n is an option if it appears before the string, otherwise it's just another string to be echoed.



        Remove the | md5sum and you'll see the output is different.






        share|improve this answer


























          6












          6








          6







          Wow, everyone's explanations are lengthy.



          It's this simple:



          -n is an option if it appears before the string, otherwise it's just another string to be echoed.



          Remove the | md5sum and you'll see the output is different.






          share|improve this answer













          Wow, everyone's explanations are lengthy.



          It's this simple:



          -n is an option if it appears before the string, otherwise it's just another string to be echoed.



          Remove the | md5sum and you'll see the output is different.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 '11 at 5:14









          MikelMikel

          40.1k10103128




          40.1k10103128























              5














              From simple inspection, this is not a bug by design...



              echo "bar" -n | md5sum

              a3f8efa5dd10e90aee0963052e3650a1


              Try this command for yourself:



              echo "bar -n" | md5sum


              You will notice that the resulting md5 is



              a3f8efa5dd10e90aee0963052e3650a1


              You just mixed-up the use of -n in echo.



              In your first sample code



              echo -n "bar" | md5sum


              -n is used to say DO NOT to put a newline after this echo.



              Your second sample



              echo "bar" -n | md5sum


              is treating -n as a literal text.






              share|improve this answer






























                5














                From simple inspection, this is not a bug by design...



                echo "bar" -n | md5sum

                a3f8efa5dd10e90aee0963052e3650a1


                Try this command for yourself:



                echo "bar -n" | md5sum


                You will notice that the resulting md5 is



                a3f8efa5dd10e90aee0963052e3650a1


                You just mixed-up the use of -n in echo.



                In your first sample code



                echo -n "bar" | md5sum


                -n is used to say DO NOT to put a newline after this echo.



                Your second sample



                echo "bar" -n | md5sum


                is treating -n as a literal text.






                share|improve this answer




























                  5












                  5








                  5







                  From simple inspection, this is not a bug by design...



                  echo "bar" -n | md5sum

                  a3f8efa5dd10e90aee0963052e3650a1


                  Try this command for yourself:



                  echo "bar -n" | md5sum


                  You will notice that the resulting md5 is



                  a3f8efa5dd10e90aee0963052e3650a1


                  You just mixed-up the use of -n in echo.



                  In your first sample code



                  echo -n "bar" | md5sum


                  -n is used to say DO NOT to put a newline after this echo.



                  Your second sample



                  echo "bar" -n | md5sum


                  is treating -n as a literal text.






                  share|improve this answer















                  From simple inspection, this is not a bug by design...



                  echo "bar" -n | md5sum

                  a3f8efa5dd10e90aee0963052e3650a1


                  Try this command for yourself:



                  echo "bar -n" | md5sum


                  You will notice that the resulting md5 is



                  a3f8efa5dd10e90aee0963052e3650a1


                  You just mixed-up the use of -n in echo.



                  In your first sample code



                  echo -n "bar" | md5sum


                  -n is used to say DO NOT to put a newline after this echo.



                  Your second sample



                  echo "bar" -n | md5sum


                  is treating -n as a literal text.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 5 hours ago









                  Rui F Ribeiro

                  41.9k1483142




                  41.9k1483142










                  answered Jan 20 '11 at 3:36









                  icasimpanicasimpan

                  304516




                  304516






























                      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%2f6162%2fwhy-is-echo-bar-n-different-from-echo-n-bar%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 rewrite equation of hyperbola in standard form

                      No ethernet ip address in my vocore2