Pattern match does not work in bash script The 2019 Stack Overflow Developer Survey Results Are InHow 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

Ubuntu Server install with full GUI

How can I define good in a religion that claims no moral authority?

What is this business jet?

How to type this arrow in math mode?

Button changing its text & action. Good or terrible?

How to charge AirPods to keep battery healthy?

Geography at the pixel level

Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?

How to translate "being like"?

Can withdrawing asylum be illegal?

How to notate time signature switching consistently every measure

Mathematics of imaging the black hole

Why can't devices on different VLANs, but on the same subnet, communicate?

Short story: child made less intelligent and less attractive

What is the motivation for a law requiring 2 parties to consent for recording a conversation

How do I free up internal storage if I don't have any apps downloaded?

Deal with toxic manager when you can't quit

Match Roman Numerals

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

If climate change impact can be observed in nature, has that had any effect on rural, i.e. farming community, perception of the scientific consensus?

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?

How can I have a shield and a way of attacking with a ranged weapon at the same time?

Loose spokes after only a few rides



Pattern match does not work in bash script



The 2019 Stack Overflow Developer Survey Results Are InHow 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;








13















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
    Apr 6 at 13:23


















13















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
    Apr 6 at 13:23














13












13








13


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 Apr 6 at 7:21









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 Apr 6 at 7:12









OgdenOgden

684




684




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
    Apr 6 at 13:23


















  • 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
    Apr 6 at 13:23

















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
Apr 6 at 13:23






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
Apr 6 at 13:23













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 Apr 6 at 7:39

























            answered Apr 6 at 7:17









            tripleeetripleee

            96.3k14134191




            96.3k14134191























                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 Apr 6 at 7:17









                    RafaelRafael

                    5,022102339




                    5,022102339





















                        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 Apr 6 at 18:24





















                            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 Apr 6 at 7:50









                            vintnesvintnes

                            1907




                            1907




                            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

                                រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

                                QGIS export composer to PDF scale the map [closed] Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Print Composer QGIS 2.6, how to export image?QGIS 2.8.1 print composer won't export all OpenCycleMap base layer tilesSave Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?Export QGIS Print Composer PDF with searchable text labelsQGIS Print Composer does not change from landscape to portrait orientation?How can I avoid map size and scale changes in print composer?Fuzzy PDF export in QGIS running on macSierra OSExport the legend into its 100% size using Print ComposerScale-dependent rendering in QGIS PDF output

                                PDF-ში გადმოწერა სანავიგაციო მენიუproject page