Bash parameter substituiton within commands

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a passing understanding of Parameter Substitution, including substrings like so



foo="Hello World";
greeting=$foo:0:6


But do I do this with commands?



greeting="Hello"
md5greeting=$(echo $greeting | md5sum :0:6)
>>b1946a


Where the output is the first 6 characters of the md5sum of the 'hello'.



How do I achieve this?










share|improve this question

























    up vote
    1
    down vote

    favorite












    I have a passing understanding of Parameter Substitution, including substrings like so



    foo="Hello World";
    greeting=$foo:0:6


    But do I do this with commands?



    greeting="Hello"
    md5greeting=$(echo $greeting | md5sum :0:6)
    >>b1946a


    Where the output is the first 6 characters of the md5sum of the 'hello'.



    How do I achieve this?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a passing understanding of Parameter Substitution, including substrings like so



      foo="Hello World";
      greeting=$foo:0:6


      But do I do this with commands?



      greeting="Hello"
      md5greeting=$(echo $greeting | md5sum :0:6)
      >>b1946a


      Where the output is the first 6 characters of the md5sum of the 'hello'.



      How do I achieve this?










      share|improve this question













      I have a passing understanding of Parameter Substitution, including substrings like so



      foo="Hello World";
      greeting=$foo:0:6


      But do I do this with commands?



      greeting="Hello"
      md5greeting=$(echo $greeting | md5sum :0:6)
      >>b1946a


      Where the output is the first 6 characters of the md5sum of the 'hello'.



      How do I achieve this?







      shell-script command-substitution variable-substitution






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Pureferret

      4431819




      4431819




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:



          var=$(echo abcdefgh | cmd1 | cmd2 | ... )
          var2="$var:2:4"


          The exception is zsh where you can convert command substitution to parameter "on the fly" and do above in one line:



          var2=$ ... )":2:4





          share|improve this answer



























            up vote
            1
            down vote













            Note that:



            echo $greeting | md5sum


            Or more reliably:



            printf '%sn' "$greeting" | md5sum


            Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:



            printf %s "$greeting" | md5sum


            For the first 6 bytes of it, with some head implementations:



            printf %s "$greeting" | md5sum | head -c 6


            or more portably:



            printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null




            share




















              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f473195%2fbash-parameter-substituiton-within-commands%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote



              accepted










              Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:



              var=$(echo abcdefgh | cmd1 | cmd2 | ... )
              var2="$var:2:4"


              The exception is zsh where you can convert command substitution to parameter "on the fly" and do above in one line:



              var2=$ ... )":2:4





              share|improve this answer
























                up vote
                3
                down vote



                accepted










                Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:



                var=$(echo abcdefgh | cmd1 | cmd2 | ... )
                var2="$var:2:4"


                The exception is zsh where you can convert command substitution to parameter "on the fly" and do above in one line:



                var2=$ ... )":2:4





                share|improve this answer






















                  up vote
                  3
                  down vote



                  accepted







                  up vote
                  3
                  down vote



                  accepted






                  Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:



                  var=$(echo abcdefgh | cmd1 | cmd2 | ... )
                  var2="$var:2:4"


                  The exception is zsh where you can convert command substitution to parameter "on the fly" and do above in one line:



                  var2=$ ... )":2:4





                  share|improve this answer












                  Parameter substitution doesn't work like that, at least not in bash. You need a real variable (parameter) and directly operate on it. So for example like that:



                  var=$(echo abcdefgh | cmd1 | cmd2 | ... )
                  var2="$var:2:4"


                  The exception is zsh where you can convert command substitution to parameter "on the fly" and do above in one line:



                  var2=$ ... )":2:4






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 40 mins ago









                  jimmij

                  29.4k867101




                  29.4k867101






















                      up vote
                      1
                      down vote













                      Note that:



                      echo $greeting | md5sum


                      Or more reliably:



                      printf '%sn' "$greeting" | md5sum


                      Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:



                      printf %s "$greeting" | md5sum


                      For the first 6 bytes of it, with some head implementations:



                      printf %s "$greeting" | md5sum | head -c 6


                      or more portably:



                      printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null




                      share
























                        up vote
                        1
                        down vote













                        Note that:



                        echo $greeting | md5sum


                        Or more reliably:



                        printf '%sn' "$greeting" | md5sum


                        Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:



                        printf %s "$greeting" | md5sum


                        For the first 6 bytes of it, with some head implementations:



                        printf %s "$greeting" | md5sum | head -c 6


                        or more portably:



                        printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null




                        share






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Note that:



                          echo $greeting | md5sum


                          Or more reliably:



                          printf '%sn' "$greeting" | md5sum


                          Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:



                          printf %s "$greeting" | md5sum


                          For the first 6 bytes of it, with some head implementations:



                          printf %s "$greeting" | md5sum | head -c 6


                          or more portably:



                          printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null




                          share












                          Note that:



                          echo $greeting | md5sum


                          Or more reliably:



                          printf '%sn' "$greeting" | md5sum


                          Does not give you the MD5 hash of the content of the variable, but of the content of the variable followed by a newline character. For the MD5 hash of the content of the variable:



                          printf %s "$greeting" | md5sum


                          For the first 6 bytes of it, with some head implementations:



                          printf %s "$greeting" | md5sum | head -c 6


                          or more portably:



                          printf %s "$greeting" | md5sum | dd bs=1 count=6 2> /dev/null





                          share











                          share


                          share










                          answered 9 mins ago









                          Stéphane Chazelas

                          287k53529867




                          287k53529867



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473195%2fbash-parameter-substituiton-within-commands%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery