Determining if the first string starts with second string

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











up vote
0
down vote

favorite












JavaScript has a function for this:



'world'.startsWith('w')
true


How can I test this with shell? I have this code:



if [ world = w ]
then
echo true
else
echo false
fi


but it fails because it is testing for equality. I would prefer using a builtin,
but any utilities from this page would be acceptable:



http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html










share|improve this question

























    up vote
    0
    down vote

    favorite












    JavaScript has a function for this:



    'world'.startsWith('w')
    true


    How can I test this with shell? I have this code:



    if [ world = w ]
    then
    echo true
    else
    echo false
    fi


    but it fails because it is testing for equality. I would prefer using a builtin,
    but any utilities from this page would be acceptable:



    http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      JavaScript has a function for this:



      'world'.startsWith('w')
      true


      How can I test this with shell? I have this code:



      if [ world = w ]
      then
      echo true
      else
      echo false
      fi


      but it fails because it is testing for equality. I would prefer using a builtin,
      but any utilities from this page would be acceptable:



      http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html










      share|improve this question













      JavaScript has a function for this:



      'world'.startsWith('w')
      true


      How can I test this with shell? I have this code:



      if [ world = w ]
      then
      echo true
      else
      echo false
      fi


      but it fails because it is testing for equality. I would prefer using a builtin,
      but any utilities from this page would be acceptable:



      http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html







      shell test






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 31 at 2:36









      Steven Penny

      2,31921635




      2,31921635




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:



          if [[ world == w* ]]; then
          echo true
          else
          echo false
          fi


          Or more tersely: [[ world == w* ]] && echo true || echo false [*]



          If you are not targetting bash specifically: use the case statement for pattern matching



          case "world" in
          w*) echo true ;;
          *) echo false ;;
          esac


          [*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.






          share|improve this answer



























            up vote
            0
            down vote













            set world


            Then:



            if [ "$1%%w*" ]
            then
            echo false
            else
            echo true
            fi


            1. Aggressively remove substring starting with w from source string

            2. If anything left, then source string does not start with second string

            Or:



            if [ "$1" = "$1#w" ]
            then
            echo false
            else
            echo true
            fi


            1. Remove w from source string

            2. Compare with source string

            3. If equal, then source string does not start with second string





            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%2f465903%2fdetermining-if-the-first-string-starts-with-second-string%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
              4
              down vote













              If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:



              if [[ world == w* ]]; then
              echo true
              else
              echo false
              fi


              Or more tersely: [[ world == w* ]] && echo true || echo false [*]



              If you are not targetting bash specifically: use the case statement for pattern matching



              case "world" in
              w*) echo true ;;
              *) echo false ;;
              esac


              [*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.






              share|improve this answer
























                up vote
                4
                down vote













                If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:



                if [[ world == w* ]]; then
                echo true
                else
                echo false
                fi


                Or more tersely: [[ world == w* ]] && echo true || echo false [*]



                If you are not targetting bash specifically: use the case statement for pattern matching



                case "world" in
                w*) echo true ;;
                *) echo false ;;
                esac


                [*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.






                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:



                  if [[ world == w* ]]; then
                  echo true
                  else
                  echo false
                  fi


                  Or more tersely: [[ world == w* ]] && echo true || echo false [*]



                  If you are not targetting bash specifically: use the case statement for pattern matching



                  case "world" in
                  w*) echo true ;;
                  *) echo false ;;
                  esac


                  [*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.






                  share|improve this answer












                  If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:



                  if [[ world == w* ]]; then
                  echo true
                  else
                  echo false
                  fi


                  Or more tersely: [[ world == w* ]] && echo true || echo false [*]



                  If you are not targetting bash specifically: use the case statement for pattern matching



                  case "world" in
                  w*) echo true ;;
                  *) echo false ;;
                  esac


                  [*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 31 at 2:51









                  glenn jackman

                  47.9k365105




                  47.9k365105






















                      up vote
                      0
                      down vote













                      set world


                      Then:



                      if [ "$1%%w*" ]
                      then
                      echo false
                      else
                      echo true
                      fi


                      1. Aggressively remove substring starting with w from source string

                      2. If anything left, then source string does not start with second string

                      Or:



                      if [ "$1" = "$1#w" ]
                      then
                      echo false
                      else
                      echo true
                      fi


                      1. Remove w from source string

                      2. Compare with source string

                      3. If equal, then source string does not start with second string





                      share|improve this answer


























                        up vote
                        0
                        down vote













                        set world


                        Then:



                        if [ "$1%%w*" ]
                        then
                        echo false
                        else
                        echo true
                        fi


                        1. Aggressively remove substring starting with w from source string

                        2. If anything left, then source string does not start with second string

                        Or:



                        if [ "$1" = "$1#w" ]
                        then
                        echo false
                        else
                        echo true
                        fi


                        1. Remove w from source string

                        2. Compare with source string

                        3. If equal, then source string does not start with second string





                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          set world


                          Then:



                          if [ "$1%%w*" ]
                          then
                          echo false
                          else
                          echo true
                          fi


                          1. Aggressively remove substring starting with w from source string

                          2. If anything left, then source string does not start with second string

                          Or:



                          if [ "$1" = "$1#w" ]
                          then
                          echo false
                          else
                          echo true
                          fi


                          1. Remove w from source string

                          2. Compare with source string

                          3. If equal, then source string does not start with second string





                          share|improve this answer














                          set world


                          Then:



                          if [ "$1%%w*" ]
                          then
                          echo false
                          else
                          echo true
                          fi


                          1. Aggressively remove substring starting with w from source string

                          2. If anything left, then source string does not start with second string

                          Or:



                          if [ "$1" = "$1#w" ]
                          then
                          echo false
                          else
                          echo true
                          fi


                          1. Remove w from source string

                          2. Compare with source string

                          3. If equal, then source string does not start with second string






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 31 at 4:54

























                          answered Aug 31 at 3:07









                          Steven Penny

                          2,31921635




                          2,31921635



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465903%2fdetermining-if-the-first-string-starts-with-second-string%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Peggy Mitchell

                              Palaiologos

                              The Forum (Inglewood, California)