The simplest way to “subtrack” a string variable in terminal?












1















Let's assume that we've a variable $test which holds a value asd 123 - what is the most simplest way to cut, for example, asd part?



I've googled this for awhile and surprised that I couldn't find an answer.










share|improve this question



























    1















    Let's assume that we've a variable $test which holds a value asd 123 - what is the most simplest way to cut, for example, asd part?



    I've googled this for awhile and surprised that I couldn't find an answer.










    share|improve this question

























      1












      1








      1








      Let's assume that we've a variable $test which holds a value asd 123 - what is the most simplest way to cut, for example, asd part?



      I've googled this for awhile and surprised that I couldn't find an answer.










      share|improve this question














      Let's assume that we've a variable $test which holds a value asd 123 - what is the most simplest way to cut, for example, asd part?



      I've googled this for awhile and surprised that I couldn't find an answer.







      command-line






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 6:03









      Cecily MillerCecily Miller

      10115




      10115






















          1 Answer
          1






          active

          oldest

          votes


















          2














          In Bourne-like shells, such as dash ( /bin/sh on Ubuntu ), bash and ksh there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash manual:



           ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The parameter expansion then results in parameter, with the smallest
          portion of the suffix matched by the pattern deleted.

          ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
          portion of the suffix matched by the pattern deleted.

          ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
          portion of the prefix matched by the pattern deleted.

          ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
          portion of the prefix matched by the pattern deleted.


          Thus, to replace asd part you can do:



          $ var="asd 123"
          $ echo ${var#asd*}
          123


          To remove 123 you can do:



          $ echo ${var%*123}
          asd


          bash goes even further with this with another form: ${parameter/string/replacement} and ${parameter//string/replacement}. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:



          $ echo ${var//123/}
          asd
          $ echo ${var//asd/}
          123


          Note that according with the syntax, the replacement part is empty string






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            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%2faskubuntu.com%2fquestions%2f1106812%2fthe-simplest-way-to-subtrack-a-string-variable-in-terminal%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            In Bourne-like shells, such as dash ( /bin/sh on Ubuntu ), bash and ksh there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash manual:



             ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The parameter expansion then results in parameter, with the smallest
            portion of the suffix matched by the pattern deleted.

            ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
            portion of the suffix matched by the pattern deleted.

            ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
            portion of the prefix matched by the pattern deleted.

            ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
            portion of the prefix matched by the pattern deleted.


            Thus, to replace asd part you can do:



            $ var="asd 123"
            $ echo ${var#asd*}
            123


            To remove 123 you can do:



            $ echo ${var%*123}
            asd


            bash goes even further with this with another form: ${parameter/string/replacement} and ${parameter//string/replacement}. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:



            $ echo ${var//123/}
            asd
            $ echo ${var//asd/}
            123


            Note that according with the syntax, the replacement part is empty string






            share|improve this answer




























              2














              In Bourne-like shells, such as dash ( /bin/sh on Ubuntu ), bash and ksh there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash manual:



               ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The parameter expansion then results in parameter, with the smallest
              portion of the suffix matched by the pattern deleted.

              ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
              portion of the suffix matched by the pattern deleted.

              ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
              portion of the prefix matched by the pattern deleted.

              ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
              portion of the prefix matched by the pattern deleted.


              Thus, to replace asd part you can do:



              $ var="asd 123"
              $ echo ${var#asd*}
              123


              To remove 123 you can do:



              $ echo ${var%*123}
              asd


              bash goes even further with this with another form: ${parameter/string/replacement} and ${parameter//string/replacement}. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:



              $ echo ${var//123/}
              asd
              $ echo ${var//asd/}
              123


              Note that according with the syntax, the replacement part is empty string






              share|improve this answer


























                2












                2








                2







                In Bourne-like shells, such as dash ( /bin/sh on Ubuntu ), bash and ksh there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash manual:



                 ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The parameter expansion then results in parameter, with the smallest
                portion of the suffix matched by the pattern deleted.

                ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
                portion of the suffix matched by the pattern deleted.

                ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
                portion of the prefix matched by the pattern deleted.

                ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
                portion of the prefix matched by the pattern deleted.


                Thus, to replace asd part you can do:



                $ var="asd 123"
                $ echo ${var#asd*}
                123


                To remove 123 you can do:



                $ echo ${var%*123}
                asd


                bash goes even further with this with another form: ${parameter/string/replacement} and ${parameter//string/replacement}. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:



                $ echo ${var//123/}
                asd
                $ echo ${var//asd/}
                123


                Note that according with the syntax, the replacement part is empty string






                share|improve this answer













                In Bourne-like shells, such as dash ( /bin/sh on Ubuntu ), bash and ksh there is something known as parameter expansion, and is a feature specified by POSIX standard. Specifically, to quote dash manual:



                 ${parameter%word}     Remove Smallest Suffix Pattern.  The word is expanded to produce a pattern.  The parameter expansion then results in parameter, with the smallest
                portion of the suffix matched by the pattern deleted.

                ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
                portion of the suffix matched by the pattern deleted.

                ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
                portion of the prefix matched by the pattern deleted.

                ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
                portion of the prefix matched by the pattern deleted.


                Thus, to replace asd part you can do:



                $ var="asd 123"
                $ echo ${var#asd*}
                123


                To remove 123 you can do:



                $ echo ${var%*123}
                asd


                bash goes even further with this with another form: ${parameter/string/replacement} and ${parameter//string/replacement}. First one replaces first occurrence of string. Second form replaces all occurrences. For instance:



                $ echo ${var//123/}
                asd
                $ echo ${var//asd/}
                123


                Note that according with the syntax, the replacement part is empty string







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 6:46









                Sergiy KolodyazhnyySergiy Kolodyazhnyy

                71.3k9147313




                71.3k9147313






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f1106812%2fthe-simplest-way-to-subtrack-a-string-variable-in-terminal%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

                    Ellipse (mathématiques)

                    Quarter-circle Tiles

                    Mont Emei