Here string adds line break












8














It seems that here string is adding line break. Is there a convenient way of removing it?



$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6 -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249 -









share|improve this question




















  • 1




    <<< also adds trailing newlines
    – Ian2thedv
    Jun 9 '16 at 14:30


















8














It seems that here string is adding line break. Is there a convenient way of removing it?



$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6 -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249 -









share|improve this question




















  • 1




    <<< also adds trailing newlines
    – Ian2thedv
    Jun 9 '16 at 14:30
















8












8








8


1





It seems that here string is adding line break. Is there a convenient way of removing it?



$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6 -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249 -









share|improve this question















It seems that here string is adding line break. Is there a convenient way of removing it?



$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6 -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249 -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249 -






bash herestring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 9 '16 at 14:39









fedorqui

165k51332377




165k51332377










asked Jun 9 '16 at 14:24









NarūnasK

1,42831533




1,42831533








  • 1




    <<< also adds trailing newlines
    – Ian2thedv
    Jun 9 '16 at 14:30
















  • 1




    <<< also adds trailing newlines
    – Ian2thedv
    Jun 9 '16 at 14:30










1




1




<<< also adds trailing newlines
– Ian2thedv
Jun 9 '16 at 14:30






<<< also adds trailing newlines
– Ian2thedv
Jun 9 '16 at 14:30














3 Answers
3






active

oldest

votes


















10














Yes, you are right: <<< adds a trailing new line.



You can see it with:



$ cat - <<< "hello" | od -c
0000000 h e l l o n
0000006


Let's compare this with the other approaches:



$ echo "hello" | od -c
0000000 h e l l o n
0000006
$ echo -n "hello" | od -c
0000000 h e l l o
0000005
$ printf "hello" | od -c
0000000 h e l l o
0000005


So we have the table:



         | adds new line |
-------------------------|
printf | No |
echo -n | No |
echo | Yes |
<<< | Yes |


From Why does a bash here-string add a trailing newline char?:




Most commands expect text input. In the unix world, a text file
consists of a sequence of lines, each ending in a
newline.
So in most cases a final newline is required. An especially common
case is to grab the output of a command with a command susbtitution,
process it in some way, then pass it to another command. The command
substitution strips final newlines; <<< puts one back.







share|improve this answer



















  • 2




    Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
    – chepner
    Jun 9 '16 at 15:03










  • @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
    – fedorqui
    Jun 9 '16 at 16:26



















3














fedorqui's helpful answer shows that and why here-strings (and also here-documents) invariably append a newline.



As for:




Is there a convenient way of removing it?




In Bash, use printf inside a process substitution as an "n-less" alternative to a here-string:



... < <(printf %s ...)


Applied to your example:



$ md5sum < <(printf %s 'test')
098f6bcd4621d373cade4e832627b4f6


Alternatively, as user202729 suggests, simply use printf %s in the pipeline, which has the added advantage of not only using a more familiar feature but also making the command work in (more strictly) POSIX-compliant shells (in scripts targeting /bin/sh):



$ printf %s 'test' | md5sum
098f6bcd4621d373cade4e832627b4f6





share|improve this answer































    1














    As a "here doc" add a newline:



    $ string="hello test"
    $ cat <<_test_ | xxd
    > $string
    > _test_
    0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


    Also a "here string" does:



    $ cat <<<"$string" | xxd
    0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


    Probably the easiest solution to get an string non-ending on newline would be printf:



    $ printf '%s' "$string" | xxd
    0000000: 6865 6c6c 6f20 7465 7374 hello test





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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%2fstackoverflow.com%2fquestions%2f37728699%2fhere-string-adds-line-break%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









      10














      Yes, you are right: <<< adds a trailing new line.



      You can see it with:



      $ cat - <<< "hello" | od -c
      0000000 h e l l o n
      0000006


      Let's compare this with the other approaches:



      $ echo "hello" | od -c
      0000000 h e l l o n
      0000006
      $ echo -n "hello" | od -c
      0000000 h e l l o
      0000005
      $ printf "hello" | od -c
      0000000 h e l l o
      0000005


      So we have the table:



               | adds new line |
      -------------------------|
      printf | No |
      echo -n | No |
      echo | Yes |
      <<< | Yes |


      From Why does a bash here-string add a trailing newline char?:




      Most commands expect text input. In the unix world, a text file
      consists of a sequence of lines, each ending in a
      newline.
      So in most cases a final newline is required. An especially common
      case is to grab the output of a command with a command susbtitution,
      process it in some way, then pass it to another command. The command
      substitution strips final newlines; <<< puts one back.







      share|improve this answer



















      • 2




        Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
        – chepner
        Jun 9 '16 at 15:03










      • @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
        – fedorqui
        Jun 9 '16 at 16:26
















      10














      Yes, you are right: <<< adds a trailing new line.



      You can see it with:



      $ cat - <<< "hello" | od -c
      0000000 h e l l o n
      0000006


      Let's compare this with the other approaches:



      $ echo "hello" | od -c
      0000000 h e l l o n
      0000006
      $ echo -n "hello" | od -c
      0000000 h e l l o
      0000005
      $ printf "hello" | od -c
      0000000 h e l l o
      0000005


      So we have the table:



               | adds new line |
      -------------------------|
      printf | No |
      echo -n | No |
      echo | Yes |
      <<< | Yes |


      From Why does a bash here-string add a trailing newline char?:




      Most commands expect text input. In the unix world, a text file
      consists of a sequence of lines, each ending in a
      newline.
      So in most cases a final newline is required. An especially common
      case is to grab the output of a command with a command susbtitution,
      process it in some way, then pass it to another command. The command
      substitution strips final newlines; <<< puts one back.







      share|improve this answer



















      • 2




        Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
        – chepner
        Jun 9 '16 at 15:03










      • @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
        – fedorqui
        Jun 9 '16 at 16:26














      10












      10








      10






      Yes, you are right: <<< adds a trailing new line.



      You can see it with:



      $ cat - <<< "hello" | od -c
      0000000 h e l l o n
      0000006


      Let's compare this with the other approaches:



      $ echo "hello" | od -c
      0000000 h e l l o n
      0000006
      $ echo -n "hello" | od -c
      0000000 h e l l o
      0000005
      $ printf "hello" | od -c
      0000000 h e l l o
      0000005


      So we have the table:



               | adds new line |
      -------------------------|
      printf | No |
      echo -n | No |
      echo | Yes |
      <<< | Yes |


      From Why does a bash here-string add a trailing newline char?:




      Most commands expect text input. In the unix world, a text file
      consists of a sequence of lines, each ending in a
      newline.
      So in most cases a final newline is required. An especially common
      case is to grab the output of a command with a command susbtitution,
      process it in some way, then pass it to another command. The command
      substitution strips final newlines; <<< puts one back.







      share|improve this answer














      Yes, you are right: <<< adds a trailing new line.



      You can see it with:



      $ cat - <<< "hello" | od -c
      0000000 h e l l o n
      0000006


      Let's compare this with the other approaches:



      $ echo "hello" | od -c
      0000000 h e l l o n
      0000006
      $ echo -n "hello" | od -c
      0000000 h e l l o
      0000005
      $ printf "hello" | od -c
      0000000 h e l l o
      0000005


      So we have the table:



               | adds new line |
      -------------------------|
      printf | No |
      echo -n | No |
      echo | Yes |
      <<< | Yes |


      From Why does a bash here-string add a trailing newline char?:




      Most commands expect text input. In the unix world, a text file
      consists of a sequence of lines, each ending in a
      newline.
      So in most cases a final newline is required. An especially common
      case is to grab the output of a command with a command susbtitution,
      process it in some way, then pass it to another command. The command
      substitution strips final newlines; <<< puts one back.








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 13 '17 at 12:36









      Community

      11




      11










      answered Jun 9 '16 at 14:35









      fedorqui

      165k51332377




      165k51332377








      • 2




        Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
        – chepner
        Jun 9 '16 at 15:03










      • @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
        – fedorqui
        Jun 9 '16 at 16:26














      • 2




        Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
        – chepner
        Jun 9 '16 at 15:03










      • @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
        – fedorqui
        Jun 9 '16 at 16:26








      2




      2




      Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
      – chepner
      Jun 9 '16 at 15:03




      Also, note that a here-string is a shortcut for a single-line here document, which always ends with a newline.
      – chepner
      Jun 9 '16 at 15:03












      @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
      – fedorqui
      Jun 9 '16 at 16:26




      @chepner this is an interesting point I wasn't aware of, that in fact may be relevant for this discussion on Here documents in documentation beta
      – fedorqui
      Jun 9 '16 at 16:26













      3














      fedorqui's helpful answer shows that and why here-strings (and also here-documents) invariably append a newline.



      As for:




      Is there a convenient way of removing it?




      In Bash, use printf inside a process substitution as an "n-less" alternative to a here-string:



      ... < <(printf %s ...)


      Applied to your example:



      $ md5sum < <(printf %s 'test')
      098f6bcd4621d373cade4e832627b4f6


      Alternatively, as user202729 suggests, simply use printf %s in the pipeline, which has the added advantage of not only using a more familiar feature but also making the command work in (more strictly) POSIX-compliant shells (in scripts targeting /bin/sh):



      $ printf %s 'test' | md5sum
      098f6bcd4621d373cade4e832627b4f6





      share|improve this answer




























        3














        fedorqui's helpful answer shows that and why here-strings (and also here-documents) invariably append a newline.



        As for:




        Is there a convenient way of removing it?




        In Bash, use printf inside a process substitution as an "n-less" alternative to a here-string:



        ... < <(printf %s ...)


        Applied to your example:



        $ md5sum < <(printf %s 'test')
        098f6bcd4621d373cade4e832627b4f6


        Alternatively, as user202729 suggests, simply use printf %s in the pipeline, which has the added advantage of not only using a more familiar feature but also making the command work in (more strictly) POSIX-compliant shells (in scripts targeting /bin/sh):



        $ printf %s 'test' | md5sum
        098f6bcd4621d373cade4e832627b4f6





        share|improve this answer


























          3












          3








          3






          fedorqui's helpful answer shows that and why here-strings (and also here-documents) invariably append a newline.



          As for:




          Is there a convenient way of removing it?




          In Bash, use printf inside a process substitution as an "n-less" alternative to a here-string:



          ... < <(printf %s ...)


          Applied to your example:



          $ md5sum < <(printf %s 'test')
          098f6bcd4621d373cade4e832627b4f6


          Alternatively, as user202729 suggests, simply use printf %s in the pipeline, which has the added advantage of not only using a more familiar feature but also making the command work in (more strictly) POSIX-compliant shells (in scripts targeting /bin/sh):



          $ printf %s 'test' | md5sum
          098f6bcd4621d373cade4e832627b4f6





          share|improve this answer














          fedorqui's helpful answer shows that and why here-strings (and also here-documents) invariably append a newline.



          As for:




          Is there a convenient way of removing it?




          In Bash, use printf inside a process substitution as an "n-less" alternative to a here-string:



          ... < <(printf %s ...)


          Applied to your example:



          $ md5sum < <(printf %s 'test')
          098f6bcd4621d373cade4e832627b4f6


          Alternatively, as user202729 suggests, simply use printf %s in the pipeline, which has the added advantage of not only using a more familiar feature but also making the command work in (more strictly) POSIX-compliant shells (in scripts targeting /bin/sh):



          $ printf %s 'test' | md5sum
          098f6bcd4621d373cade4e832627b4f6






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 24 '18 at 12:39

























          answered Jun 10 '16 at 23:07









          mklement0

          126k20240268




          126k20240268























              1














              As a "here doc" add a newline:



              $ string="hello test"
              $ cat <<_test_ | xxd
              > $string
              > _test_
              0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


              Also a "here string" does:



              $ cat <<<"$string" | xxd
              0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


              Probably the easiest solution to get an string non-ending on newline would be printf:



              $ printf '%s' "$string" | xxd
              0000000: 6865 6c6c 6f20 7465 7374 hello test





              share|improve this answer


























                1














                As a "here doc" add a newline:



                $ string="hello test"
                $ cat <<_test_ | xxd
                > $string
                > _test_
                0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                Also a "here string" does:



                $ cat <<<"$string" | xxd
                0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                Probably the easiest solution to get an string non-ending on newline would be printf:



                $ printf '%s' "$string" | xxd
                0000000: 6865 6c6c 6f20 7465 7374 hello test





                share|improve this answer
























                  1












                  1








                  1






                  As a "here doc" add a newline:



                  $ string="hello test"
                  $ cat <<_test_ | xxd
                  > $string
                  > _test_
                  0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                  Also a "here string" does:



                  $ cat <<<"$string" | xxd
                  0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                  Probably the easiest solution to get an string non-ending on newline would be printf:



                  $ printf '%s' "$string" | xxd
                  0000000: 6865 6c6c 6f20 7465 7374 hello test





                  share|improve this answer












                  As a "here doc" add a newline:



                  $ string="hello test"
                  $ cat <<_test_ | xxd
                  > $string
                  > _test_
                  0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                  Also a "here string" does:



                  $ cat <<<"$string" | xxd
                  0000000: 6865 6c6c 6f20 7465 7374 0a hello test.


                  Probably the easiest solution to get an string non-ending on newline would be printf:



                  $ printf '%s' "$string" | xxd
                  0000000: 6865 6c6c 6f20 7465 7374 hello test






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 10 '16 at 22:21







                  user2350426





































                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


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





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2fstackoverflow.com%2fquestions%2f37728699%2fhere-string-adds-line-break%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

                      MongoDB - Not Authorized To Execute Command

                      Npm cannot find a required file even through it is in the searched directory

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith