Bash parameter substituiton within commands

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











up vote
3
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

















  • 1




    echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
    – glenn jackman
    Oct 4 at 14:37














up vote
3
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

















  • 1




    echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
    – glenn jackman
    Oct 4 at 14:37












up vote
3
down vote

favorite









up vote
3
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 Oct 4 at 10:35









Pureferret

4531819




4531819







  • 1




    echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
    – glenn jackman
    Oct 4 at 14:37












  • 1




    echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
    – glenn jackman
    Oct 4 at 14:37







1




1




echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
– glenn jackman
Oct 4 at 14:37




echo "$greeting" | md5sum | cut -c 1-6 would work if you know the output will only be a single line.
– glenn jackman
Oct 4 at 14:37










2 Answers
2






active

oldest

votes

















up vote
5
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=$ cdm1 





share|improve this answer



























    up vote
    4
    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|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',
      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
      5
      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=$ cdm1 





      share|improve this answer
























        up vote
        5
        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=$ cdm1 





        share|improve this answer






















          up vote
          5
          down vote



          accepted







          up vote
          5
          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=$ cdm1 





          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=$ cdm1 






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 4 at 10:58









          jimmij

          29.5k867101




          29.5k867101






















              up vote
              4
              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|improve this answer
























                up vote
                4
                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|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  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|improve this answer












                  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|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 4 at 11:28









                  Stéphane Chazelas

                  287k53531868




                  287k53531868



























                       

                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay