Parenthesis works in bash shell itself, but not in bash script












10














I can run this command from my command line prompt:



cp -r folder/!(exclude-me) ./


To recursively copy all contents of folder except for the subdirectory named exclude-me into the current directory. This works exactly as intended. However, I need this to work in a bash script I've written, where I have this:



if [ -d "folder" ]; then
cp -r folder/!(exclude-me) ./
rm -rf folder
fi


But when I run the script:



bash my-script.sh


I get this:



my-script.sh: line 30: syntax error near unexpected token `('
my-script.sh: line 30: ` cp -r folder/!(exclude-me) ./'


And I'm at a loss as to why it works from the command prompt, but the exact same line doesn't work in a bash script.










share|improve this question





























    10














    I can run this command from my command line prompt:



    cp -r folder/!(exclude-me) ./


    To recursively copy all contents of folder except for the subdirectory named exclude-me into the current directory. This works exactly as intended. However, I need this to work in a bash script I've written, where I have this:



    if [ -d "folder" ]; then
    cp -r folder/!(exclude-me) ./
    rm -rf folder
    fi


    But when I run the script:



    bash my-script.sh


    I get this:



    my-script.sh: line 30: syntax error near unexpected token `('
    my-script.sh: line 30: ` cp -r folder/!(exclude-me) ./'


    And I'm at a loss as to why it works from the command prompt, but the exact same line doesn't work in a bash script.










    share|improve this question



























      10












      10








      10







      I can run this command from my command line prompt:



      cp -r folder/!(exclude-me) ./


      To recursively copy all contents of folder except for the subdirectory named exclude-me into the current directory. This works exactly as intended. However, I need this to work in a bash script I've written, where I have this:



      if [ -d "folder" ]; then
      cp -r folder/!(exclude-me) ./
      rm -rf folder
      fi


      But when I run the script:



      bash my-script.sh


      I get this:



      my-script.sh: line 30: syntax error near unexpected token `('
      my-script.sh: line 30: ` cp -r folder/!(exclude-me) ./'


      And I'm at a loss as to why it works from the command prompt, but the exact same line doesn't work in a bash script.










      share|improve this question















      I can run this command from my command line prompt:



      cp -r folder/!(exclude-me) ./


      To recursively copy all contents of folder except for the subdirectory named exclude-me into the current directory. This works exactly as intended. However, I need this to work in a bash script I've written, where I have this:



      if [ -d "folder" ]; then
      cp -r folder/!(exclude-me) ./
      rm -rf folder
      fi


      But when I run the script:



      bash my-script.sh


      I get this:



      my-script.sh: line 30: syntax error near unexpected token `('
      my-script.sh: line 30: ` cp -r folder/!(exclude-me) ./'


      And I'm at a loss as to why it works from the command prompt, but the exact same line doesn't work in a bash script.







      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 20 '18 at 0:14









      Rui F Ribeiro

      39k1479130




      39k1479130










      asked Sep 25 '14 at 18:20









      nzifnab

      15316




      15316






















          2 Answers
          2






          active

          oldest

          votes


















          7














          That's because the syntax you're using depends on a particular bash feature which is not activated by default for non-interactive shells (scripts). You can activate it by adding the relevant command to your script:



          ## Enable extended globbing features
          shopt -s extglob

          if [ -d "folder" ]; then
          cp -r folder/!(exclude-me) ./
          rm -rf folder
          fi


          This is the relevant section of man bash:



             If the extglob shell option is enabled using the shopt builtin, several
          extended pattern matching operators are recognized. In the following
          description, a pattern-list is a list of one or more patterns separated
          by a |. Composite patterns may be formed using one or more of the fol‐
          lowing sub-patterns:

          ?(pattern-list)
          Matches zero or one occurrence of the given patterns
          *(pattern-list)
          Matches zero or more occurrences of the given patterns
          +(pattern-list)
          Matches one or more occurrences of the given patterns
          @(pattern-list)
          Matches one of the given patterns
          !(pattern-list)
          Matches anything except one of the given patterns





          share|improve this answer





























            1














            Add this line near top of your script:



            shopt -s extglob


            !(...) is an extended pattern matching feature, you need extglob option enable to use it. See shopt builtin for more details.






            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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f157541%2fparenthesis-works-in-bash-shell-itself-but-not-in-bash-script%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              That's because the syntax you're using depends on a particular bash feature which is not activated by default for non-interactive shells (scripts). You can activate it by adding the relevant command to your script:



              ## Enable extended globbing features
              shopt -s extglob

              if [ -d "folder" ]; then
              cp -r folder/!(exclude-me) ./
              rm -rf folder
              fi


              This is the relevant section of man bash:



                 If the extglob shell option is enabled using the shopt builtin, several
              extended pattern matching operators are recognized. In the following
              description, a pattern-list is a list of one or more patterns separated
              by a |. Composite patterns may be formed using one or more of the fol‐
              lowing sub-patterns:

              ?(pattern-list)
              Matches zero or one occurrence of the given patterns
              *(pattern-list)
              Matches zero or more occurrences of the given patterns
              +(pattern-list)
              Matches one or more occurrences of the given patterns
              @(pattern-list)
              Matches one of the given patterns
              !(pattern-list)
              Matches anything except one of the given patterns





              share|improve this answer


























                7














                That's because the syntax you're using depends on a particular bash feature which is not activated by default for non-interactive shells (scripts). You can activate it by adding the relevant command to your script:



                ## Enable extended globbing features
                shopt -s extglob

                if [ -d "folder" ]; then
                cp -r folder/!(exclude-me) ./
                rm -rf folder
                fi


                This is the relevant section of man bash:



                   If the extglob shell option is enabled using the shopt builtin, several
                extended pattern matching operators are recognized. In the following
                description, a pattern-list is a list of one or more patterns separated
                by a |. Composite patterns may be formed using one or more of the fol‐
                lowing sub-patterns:

                ?(pattern-list)
                Matches zero or one occurrence of the given patterns
                *(pattern-list)
                Matches zero or more occurrences of the given patterns
                +(pattern-list)
                Matches one or more occurrences of the given patterns
                @(pattern-list)
                Matches one of the given patterns
                !(pattern-list)
                Matches anything except one of the given patterns





                share|improve this answer
























                  7












                  7








                  7






                  That's because the syntax you're using depends on a particular bash feature which is not activated by default for non-interactive shells (scripts). You can activate it by adding the relevant command to your script:



                  ## Enable extended globbing features
                  shopt -s extglob

                  if [ -d "folder" ]; then
                  cp -r folder/!(exclude-me) ./
                  rm -rf folder
                  fi


                  This is the relevant section of man bash:



                     If the extglob shell option is enabled using the shopt builtin, several
                  extended pattern matching operators are recognized. In the following
                  description, a pattern-list is a list of one or more patterns separated
                  by a |. Composite patterns may be formed using one or more of the fol‐
                  lowing sub-patterns:

                  ?(pattern-list)
                  Matches zero or one occurrence of the given patterns
                  *(pattern-list)
                  Matches zero or more occurrences of the given patterns
                  +(pattern-list)
                  Matches one or more occurrences of the given patterns
                  @(pattern-list)
                  Matches one of the given patterns
                  !(pattern-list)
                  Matches anything except one of the given patterns





                  share|improve this answer












                  That's because the syntax you're using depends on a particular bash feature which is not activated by default for non-interactive shells (scripts). You can activate it by adding the relevant command to your script:



                  ## Enable extended globbing features
                  shopt -s extglob

                  if [ -d "folder" ]; then
                  cp -r folder/!(exclude-me) ./
                  rm -rf folder
                  fi


                  This is the relevant section of man bash:



                     If the extglob shell option is enabled using the shopt builtin, several
                  extended pattern matching operators are recognized. In the following
                  description, a pattern-list is a list of one or more patterns separated
                  by a |. Composite patterns may be formed using one or more of the fol‐
                  lowing sub-patterns:

                  ?(pattern-list)
                  Matches zero or one occurrence of the given patterns
                  *(pattern-list)
                  Matches zero or more occurrences of the given patterns
                  +(pattern-list)
                  Matches one or more occurrences of the given patterns
                  @(pattern-list)
                  Matches one of the given patterns
                  !(pattern-list)
                  Matches anything except one of the given patterns






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 25 '14 at 18:24









                  terdon

                  128k31249423




                  128k31249423

























                      1














                      Add this line near top of your script:



                      shopt -s extglob


                      !(...) is an extended pattern matching feature, you need extglob option enable to use it. See shopt builtin for more details.






                      share|improve this answer


























                        1














                        Add this line near top of your script:



                        shopt -s extglob


                        !(...) is an extended pattern matching feature, you need extglob option enable to use it. See shopt builtin for more details.






                        share|improve this answer
























                          1












                          1








                          1






                          Add this line near top of your script:



                          shopt -s extglob


                          !(...) is an extended pattern matching feature, you need extglob option enable to use it. See shopt builtin for more details.






                          share|improve this answer












                          Add this line near top of your script:



                          shopt -s extglob


                          !(...) is an extended pattern matching feature, you need extglob option enable to use it. See shopt builtin for more details.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 25 '14 at 18:22









                          cuonglm

                          102k23201301




                          102k23201301






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2funix.stackexchange.com%2fquestions%2f157541%2fparenthesis-works-in-bash-shell-itself-but-not-in-bash-script%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

                              Morgemoulin

                              Scott Moir

                              Souastre