Pattern match does not work in bash scriptHow can I use inverse or negative wildcards when pattern matching in a unix/linux shell?List all files that do not match pattern using lsWhy would I not leave extglob enabled in bash?Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to concatenate string variables in BashReliable way for a Bash script to get the full path to itselfPassing parameters to a Bash functionEcho newline in Bash prints literal n

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Why was the small council so happy for Tyrion to become the Master of Coin?

What is the word for reserving something for yourself before others do?

Arthur Somervell: 1000 Exercises - Meaning of this notation

Languages that we cannot (dis)prove to be Context-Free

tikz: show 0 at the axis origin

What are the differences between the usage of 'it' and 'they'?

Font hinting is lost in Chrome-like browsers (for some languages )

Problem of parity - Can we draw a closed path made up of 20 line segments...

Which models of the Boeing 737 are still in production?

Why, historically, did Gödel think CH was false?

What's the output of a record cartridge playing an out-of-speed record

To string or not to string

Is this a crack on the carbon frame?

Test if tikzmark exists on same page

Today is the Center

Is it important to consider tone, melody, and musical form while writing a song?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

How do I create uniquely male characters?

How does one intimidate enemies without having the capacity for violence?

Python: next in for loop

What typically incentivizes a professor to change jobs to a lower ranking university?

Why doesn't H₄O²⁺ exist?

Is it unprofessional to ask if a job posting on GlassDoor is real?



Pattern match does not work in bash script


How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?List all files that do not match pattern using lsWhy would I not leave extglob enabled in bash?Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to concatenate string variables in BashReliable way for a Bash script to get the full path to itselfPassing parameters to a Bash functionEcho newline in Bash prints literal n






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








12















Using the pattern match !("file1") does not work within a bash script but will work on the command line.



For example:



ls !("file1"|"file2")


This will list all files in directory except file1 and file2.



When that line is executed in a script this error is displayed:



./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '


Regardless what is used rm -v !("file1"). The same error takes place. What is going on here why does this not work in a script?










share|improve this question









New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

    – jww
    11 hours ago


















12















Using the pattern match !("file1") does not work within a bash script but will work on the command line.



For example:



ls !("file1"|"file2")


This will list all files in directory except file1 and file2.



When that line is executed in a script this error is displayed:



./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '


Regardless what is used rm -v !("file1"). The same error takes place. What is going on here why does this not work in a script?










share|improve this question









New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

    – jww
    11 hours ago














12












12








12


1






Using the pattern match !("file1") does not work within a bash script but will work on the command line.



For example:



ls !("file1"|"file2")


This will list all files in directory except file1 and file2.



When that line is executed in a script this error is displayed:



./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '


Regardless what is used rm -v !("file1"). The same error takes place. What is going on here why does this not work in a script?










share|improve this question









New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Using the pattern match !("file1") does not work within a bash script but will work on the command line.



For example:



ls !("file1"|"file2")


This will list all files in directory except file1 and file2.



When that line is executed in a script this error is displayed:



./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '


Regardless what is used rm -v !("file1"). The same error takes place. What is going on here why does this not work in a script?







linux bash glob extglob






share|improve this question









New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 17 hours ago









James Brown

20.5k42037




20.5k42037






New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 18 hours ago









OgdenOgden

634




634




New contributor




Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ogden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

    – jww
    11 hours ago


















  • Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

    – jww
    11 hours ago

















Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

– jww
11 hours ago






Possible duplicate of How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?, List all files that do not match pattern using ls, etc. And related is Why would I not leave extglob enabled in bash?

– jww
11 hours ago













3 Answers
3






active

oldest

votes


















12














The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.



shopt -s extglob


Scripts should not use ls though I imagine you were using it merely as a placeholder here.






share|improve this answer
































    5














    Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:



    find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete


    before running this command with -delete ensure the output is correct






    share|improve this answer






























      3














      Method with default settings and no external procs:



      for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done





      share|improve this answer










      New contributor




      vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        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
        );



        );






        Ogden is a new contributor. Be nice, and check out our Code of Conduct.









        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55546727%2fpattern-match-does-not-work-in-bash-script%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        12














        The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.



        shopt -s extglob


        Scripts should not use ls though I imagine you were using it merely as a placeholder here.






        share|improve this answer





























          12














          The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.



          shopt -s extglob


          Scripts should not use ls though I imagine you were using it merely as a placeholder here.






          share|improve this answer



























            12












            12








            12







            The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.



            shopt -s extglob


            Scripts should not use ls though I imagine you were using it merely as a placeholder here.






            share|improve this answer















            The extended glob syntax you are trying to use is turned off by default; you have to enable it separately in each script where you want to use it.



            shopt -s extglob


            Scripts should not use ls though I imagine you were using it merely as a placeholder here.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 17 hours ago

























            answered 18 hours ago









            tripleeetripleee

            96k13133190




            96k13133190























                5














                Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:



                find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete


                before running this command with -delete ensure the output is correct






                share|improve this answer



























                  5














                  Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:



                  find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete


                  before running this command with -delete ensure the output is correct






                  share|improve this answer

























                    5












                    5








                    5







                    Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:



                    find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete


                    before running this command with -delete ensure the output is correct






                    share|improve this answer













                    Globbing doesn't work that way unless you enable extglob shell opt. Instead, I recommend using find:



                    find . -maxdepth 1 -not -name '<NAME>' -or -name '<NAME>' -delete


                    before running this command with -delete ensure the output is correct







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 18 hours ago









                    RafaelRafael

                    4,962102339




                    4,962102339





















                        3














                        Method with default settings and no external procs:



                        for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done





                        share|improve this answer










                        New contributor




                        vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.
























                          3














                          Method with default settings and no external procs:



                          for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done





                          share|improve this answer










                          New contributor




                          vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






















                            3












                            3








                            3







                            Method with default settings and no external procs:



                            for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done





                            share|improve this answer










                            New contributor




                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.










                            Method with default settings and no external procs:



                            for f in *; do [[ $f =~ ^file[12]$ ]] || echo "$f"; done






                            share|improve this answer










                            New contributor




                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer








                            edited 6 hours ago





















                            New contributor




                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 17 hours ago









                            vintnesvintnes

                            765




                            765




                            New contributor




                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            vintnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.




















                                Ogden is a new contributor. Be nice, and check out our Code of Conduct.









                                draft saved

                                draft discarded


















                                Ogden is a new contributor. Be nice, and check out our Code of Conduct.












                                Ogden is a new contributor. Be nice, and check out our Code of Conduct.











                                Ogden is a new contributor. Be nice, and check out our Code of Conduct.














                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f55546727%2fpattern-match-does-not-work-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

                                Reverse int within the 32-bit signed integer range: [−2^31, 2^31 − 1]Combining two 32-bit integers into one 64-bit integerDetermine if an int is within rangeLossy packing 32 bit integer to 16 bitComputing the square root of a 64-bit integerKeeping integer addition within boundsSafe multiplication of two 64-bit signed integersLeetcode 10: Regular Expression MatchingSigned integer-to-ascii x86_64 assembler macroReverse the digits of an Integer“Add two numbers given in reverse order from a linked list”

                                Category:Fedor von Bock Media in category "Fedor von Bock"Navigation menuUpload mediaISNI: 0000 0000 5511 3417VIAF ID: 24712551GND ID: 119294796Library of Congress authority ID: n96068363BnF ID: 12534305fSUDOC authorities ID: 034604189Open Library ID: OL338253ANKCR AUT ID: jn19990000869National Library of Israel ID: 000514068National Thesaurus for Author Names ID: 341574317ReasonatorScholiaStatistics

                                Kiel Indholdsfortegnelse Historie | Transport og færgeforbindelser | Sejlsport og anden sport | Kultur | Kendte personer fra Kiel | Noter | Litteratur | Eksterne henvisninger | Navigationsmenuwww.kiel.de54°19′31″N 10°8′26″Ø / 54.32528°N 10.14056°Ø / 54.32528; 10.14056Oberbürgermeister Dr. Ulf Kämpferwww.statistik-nord.deDen danske Stats StatistikKiels hjemmesiderrrWorldCat312794080n790547494030481-4