What is the concept of Shortest Sub-string Match in Unix Shell?

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











up vote
2
down vote

favorite












I'm using following script for Shortest Sub-string Match in String handling.



filename="bash.string.txt"

echo $filename#*.


It gives following output.



string.txt


Here is an explanation of above example (Link: https://www.thegeekstuff.com/2010/07/bash-string-manipulation):




The above example deletes the shortest match of $substring from front
of $string. In the first echo statement substring ‘*.’ matches the
characters and a dot, and # strips from the front of the string, so it
strips the substring “bash.” from the variable called filename.




Then I changed the code as below:



filename="bashshell.string.txt"

echo $filename#*.


I just extended the first string from bash. to bashshell.
and expecting the output "bashshell.txt" according to explanation given above.
But instead it gives me same output as first example.



i.e. string.txt



So do I misunderstood the concept? If yes than how it actually works?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm using following script for Shortest Sub-string Match in String handling.



    filename="bash.string.txt"

    echo $filename#*.


    It gives following output.



    string.txt


    Here is an explanation of above example (Link: https://www.thegeekstuff.com/2010/07/bash-string-manipulation):




    The above example deletes the shortest match of $substring from front
    of $string. In the first echo statement substring ‘*.’ matches the
    characters and a dot, and # strips from the front of the string, so it
    strips the substring “bash.” from the variable called filename.




    Then I changed the code as below:



    filename="bashshell.string.txt"

    echo $filename#*.


    I just extended the first string from bash. to bashshell.
    and expecting the output "bashshell.txt" according to explanation given above.
    But instead it gives me same output as first example.



    i.e. string.txt



    So do I misunderstood the concept? If yes than how it actually works?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm using following script for Shortest Sub-string Match in String handling.



      filename="bash.string.txt"

      echo $filename#*.


      It gives following output.



      string.txt


      Here is an explanation of above example (Link: https://www.thegeekstuff.com/2010/07/bash-string-manipulation):




      The above example deletes the shortest match of $substring from front
      of $string. In the first echo statement substring ‘*.’ matches the
      characters and a dot, and # strips from the front of the string, so it
      strips the substring “bash.” from the variable called filename.




      Then I changed the code as below:



      filename="bashshell.string.txt"

      echo $filename#*.


      I just extended the first string from bash. to bashshell.
      and expecting the output "bashshell.txt" according to explanation given above.
      But instead it gives me same output as first example.



      i.e. string.txt



      So do I misunderstood the concept? If yes than how it actually works?










      share|improve this question















      I'm using following script for Shortest Sub-string Match in String handling.



      filename="bash.string.txt"

      echo $filename#*.


      It gives following output.



      string.txt


      Here is an explanation of above example (Link: https://www.thegeekstuff.com/2010/07/bash-string-manipulation):




      The above example deletes the shortest match of $substring from front
      of $string. In the first echo statement substring ‘*.’ matches the
      characters and a dot, and # strips from the front of the string, so it
      strips the substring “bash.” from the variable called filename.




      Then I changed the code as below:



      filename="bashshell.string.txt"

      echo $filename#*.


      I just extended the first string from bash. to bashshell.
      and expecting the output "bashshell.txt" according to explanation given above.
      But instead it gives me same output as first example.



      i.e. string.txt



      So do I misunderstood the concept? If yes than how it actually works?







      bash shell string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 7 at 12:49









      slm♦

      238k65491662




      238k65491662










      asked Aug 7 at 12:31









      Dip

      198117




      198117




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote














          So do I misunderstood the concept? If yes than how it actually works?




          Yes the notation $var#*. is removing everything from the beginning of the string up to the character dot (.). It's doing what you asked of it, your pattern was star dot:



          *.


          So it will match everything up to the 1st dot from the start of the string, the one after the word bash.



          bash.string.txt
          ^---------------- it's splitting here


          Examples



          $ str="bash.string.txt"
          $ echo "$str#*."
          string.txt

          $ str="bash1.string.txt"
          $ echo "$str#*."
          string.txt

          $ str="bash1.string1.txt"
          $ echo "$str#*."
          string1.txt


          See when I put the 1 on the left side of the 1st .. This notation is truncating everything up to the 1st dot.






          share|improve this answer


















          • 1




            compared to $str##*. which will replace everything until the last dot.
            – RoVo
            Aug 7 at 13:04

















          up vote
          2
          down vote













          The tutorial's use of the word "substring" is slightly misleading. When using $variable#pattern, we're dealing with matching and deleting a prefix string (and with $variable%pattern a suffix string).



          You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.



          The POSIX standard defines this particular parameter expansion as




          $parameter#[word]



          Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.




          Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:



          suffix=$filename##*. # remove everything to the *last* dot
          echo "$filename%%.*.$suffix" # remove everything from the first dot and add suffix


          The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.



          Alternatively with bash:



          echo "$filename/string./"


          This removes the (first occurrence of the) given string anywhere within the value of $filename.






          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%2f461058%2fwhat-is-the-concept-of-shortest-sub-string-match-in-unix-shell%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














            So do I misunderstood the concept? If yes than how it actually works?




            Yes the notation $var#*. is removing everything from the beginning of the string up to the character dot (.). It's doing what you asked of it, your pattern was star dot:



            *.


            So it will match everything up to the 1st dot from the start of the string, the one after the word bash.



            bash.string.txt
            ^---------------- it's splitting here


            Examples



            $ str="bash.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string1.txt"
            $ echo "$str#*."
            string1.txt


            See when I put the 1 on the left side of the 1st .. This notation is truncating everything up to the 1st dot.






            share|improve this answer


















            • 1




              compared to $str##*. which will replace everything until the last dot.
              – RoVo
              Aug 7 at 13:04














            up vote
            3
            down vote














            So do I misunderstood the concept? If yes than how it actually works?




            Yes the notation $var#*. is removing everything from the beginning of the string up to the character dot (.). It's doing what you asked of it, your pattern was star dot:



            *.


            So it will match everything up to the 1st dot from the start of the string, the one after the word bash.



            bash.string.txt
            ^---------------- it's splitting here


            Examples



            $ str="bash.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string1.txt"
            $ echo "$str#*."
            string1.txt


            See when I put the 1 on the left side of the 1st .. This notation is truncating everything up to the 1st dot.






            share|improve this answer


















            • 1




              compared to $str##*. which will replace everything until the last dot.
              – RoVo
              Aug 7 at 13:04












            up vote
            3
            down vote










            up vote
            3
            down vote










            So do I misunderstood the concept? If yes than how it actually works?




            Yes the notation $var#*. is removing everything from the beginning of the string up to the character dot (.). It's doing what you asked of it, your pattern was star dot:



            *.


            So it will match everything up to the 1st dot from the start of the string, the one after the word bash.



            bash.string.txt
            ^---------------- it's splitting here


            Examples



            $ str="bash.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string1.txt"
            $ echo "$str#*."
            string1.txt


            See when I put the 1 on the left side of the 1st .. This notation is truncating everything up to the 1st dot.






            share|improve this answer















            So do I misunderstood the concept? If yes than how it actually works?




            Yes the notation $var#*. is removing everything from the beginning of the string up to the character dot (.). It's doing what you asked of it, your pattern was star dot:



            *.


            So it will match everything up to the 1st dot from the start of the string, the one after the word bash.



            bash.string.txt
            ^---------------- it's splitting here


            Examples



            $ str="bash.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string.txt"
            $ echo "$str#*."
            string.txt

            $ str="bash1.string1.txt"
            $ echo "$str#*."
            string1.txt


            See when I put the 1 on the left side of the 1st .. This notation is truncating everything up to the 1st dot.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 7 at 14:17









            ilkkachu

            50.9k678140




            50.9k678140










            answered Aug 7 at 12:53









            slm♦

            238k65491662




            238k65491662







            • 1




              compared to $str##*. which will replace everything until the last dot.
              – RoVo
              Aug 7 at 13:04












            • 1




              compared to $str##*. which will replace everything until the last dot.
              – RoVo
              Aug 7 at 13:04







            1




            1




            compared to $str##*. which will replace everything until the last dot.
            – RoVo
            Aug 7 at 13:04




            compared to $str##*. which will replace everything until the last dot.
            – RoVo
            Aug 7 at 13:04












            up vote
            2
            down vote













            The tutorial's use of the word "substring" is slightly misleading. When using $variable#pattern, we're dealing with matching and deleting a prefix string (and with $variable%pattern a suffix string).



            You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.



            The POSIX standard defines this particular parameter expansion as




            $parameter#[word]



            Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.




            Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:



            suffix=$filename##*. # remove everything to the *last* dot
            echo "$filename%%.*.$suffix" # remove everything from the first dot and add suffix


            The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.



            Alternatively with bash:



            echo "$filename/string./"


            This removes the (first occurrence of the) given string anywhere within the value of $filename.






            share|improve this answer


























              up vote
              2
              down vote













              The tutorial's use of the word "substring" is slightly misleading. When using $variable#pattern, we're dealing with matching and deleting a prefix string (and with $variable%pattern a suffix string).



              You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.



              The POSIX standard defines this particular parameter expansion as




              $parameter#[word]



              Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.




              Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:



              suffix=$filename##*. # remove everything to the *last* dot
              echo "$filename%%.*.$suffix" # remove everything from the first dot and add suffix


              The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.



              Alternatively with bash:



              echo "$filename/string./"


              This removes the (first occurrence of the) given string anywhere within the value of $filename.






              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote









                The tutorial's use of the word "substring" is slightly misleading. When using $variable#pattern, we're dealing with matching and deleting a prefix string (and with $variable%pattern a suffix string).



                You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.



                The POSIX standard defines this particular parameter expansion as




                $parameter#[word]



                Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.




                Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:



                suffix=$filename##*. # remove everything to the *last* dot
                echo "$filename%%.*.$suffix" # remove everything from the first dot and add suffix


                The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.



                Alternatively with bash:



                echo "$filename/string./"


                This removes the (first occurrence of the) given string anywhere within the value of $filename.






                share|improve this answer














                The tutorial's use of the word "substring" is slightly misleading. When using $variable#pattern, we're dealing with matching and deleting a prefix string (and with $variable%pattern a suffix string).



                You removed the shortest prefix string matching *. from the two strings bash.string.txt and bashshell.string.txt. The result for both strings is the same, string.txt, because the pattern *. matches up to and including the first dot in the string.



                The POSIX standard defines this particular parameter expansion as




                $parameter#[word]



                Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted #.




                Had you wanted to get the result bashshell.txt, you would have had to remove the string .string or string. from the middle of the string. This could be done in two steps with standard parameter expansions:



                suffix=$filename##*. # remove everything to the *last* dot
                echo "$filename%%.*.$suffix" # remove everything from the first dot and add suffix


                The ## and %% variations of the parameter expansion removes the longest matching prefix and suffix strings respectively.



                Alternatively with bash:



                echo "$filename/string./"


                This removes the (first occurrence of the) given string anywhere within the value of $filename.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 7 at 13:52

























                answered Aug 7 at 12:38









                Kusalananda

                106k14209327




                106k14209327



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461058%2fwhat-is-the-concept-of-shortest-sub-string-match-in-unix-shell%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