How is the wildcard * interpreted as a command?

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I know how * is interpreted in commands like ls for example. But when trying to run * as a command the shell tries to execute the first file or directory in your working directory. Why is this? How does the shell understand *?










share|improve this question






























    3















    I know how * is interpreted in commands like ls for example. But when trying to run * as a command the shell tries to execute the first file or directory in your working directory. Why is this? How does the shell understand *?










    share|improve this question


























      3












      3








      3








      I know how * is interpreted in commands like ls for example. But when trying to run * as a command the shell tries to execute the first file or directory in your working directory. Why is this? How does the shell understand *?










      share|improve this question
















      I know how * is interpreted in commands like ls for example. But when trying to run * as a command the shell tries to execute the first file or directory in your working directory. Why is this? How does the shell understand *?







      bash wildcards






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 11 at 20:31









      Glorfindel

      3371511




      3371511










      asked Mar 11 at 19:07









      osmakosmak

      434




      434




















          1 Answer
          1






          active

          oldest

          votes


















          8














          Unlike in some other operating systems, in Unixes, it's the shell that expands filename wildcards. It expands parameter expansions and globs first, then uses the (now) first word as the name for the command to run.



          This is also why files named with a leading dash can be troublesome: a glob like * will expand to the file names, and a name starting with a dash may be taken as an option. (The scary example being that a file called -r in the working directory would make rm * remove everything recursively...)



          Usually, one wouldn't use * as the first item on any command line, though, so having the first file determine the command to run in that case isn't likely to cause problems.






          share|improve this answer




















          • 1





            I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

            – Michael Homer
            Mar 11 at 19:31











          • OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

            – osmak
            Mar 11 at 19:42











          • @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

            – ilkkachu
            Mar 11 at 19:56











          • @osmak That is a correct understanding.

            – Kusalananda
            Mar 11 at 19:56






          • 2





            A file named -r is bad, but one named -rf is even worse.

            – Monty Harder
            Mar 11 at 21:57











          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%2f505707%2fhow-is-the-wildcard-interpreted-as-a-command%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









          8














          Unlike in some other operating systems, in Unixes, it's the shell that expands filename wildcards. It expands parameter expansions and globs first, then uses the (now) first word as the name for the command to run.



          This is also why files named with a leading dash can be troublesome: a glob like * will expand to the file names, and a name starting with a dash may be taken as an option. (The scary example being that a file called -r in the working directory would make rm * remove everything recursively...)



          Usually, one wouldn't use * as the first item on any command line, though, so having the first file determine the command to run in that case isn't likely to cause problems.






          share|improve this answer




















          • 1





            I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

            – Michael Homer
            Mar 11 at 19:31











          • OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

            – osmak
            Mar 11 at 19:42











          • @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

            – ilkkachu
            Mar 11 at 19:56











          • @osmak That is a correct understanding.

            – Kusalananda
            Mar 11 at 19:56






          • 2





            A file named -r is bad, but one named -rf is even worse.

            – Monty Harder
            Mar 11 at 21:57















          8














          Unlike in some other operating systems, in Unixes, it's the shell that expands filename wildcards. It expands parameter expansions and globs first, then uses the (now) first word as the name for the command to run.



          This is also why files named with a leading dash can be troublesome: a glob like * will expand to the file names, and a name starting with a dash may be taken as an option. (The scary example being that a file called -r in the working directory would make rm * remove everything recursively...)



          Usually, one wouldn't use * as the first item on any command line, though, so having the first file determine the command to run in that case isn't likely to cause problems.






          share|improve this answer




















          • 1





            I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

            – Michael Homer
            Mar 11 at 19:31











          • OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

            – osmak
            Mar 11 at 19:42











          • @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

            – ilkkachu
            Mar 11 at 19:56











          • @osmak That is a correct understanding.

            – Kusalananda
            Mar 11 at 19:56






          • 2





            A file named -r is bad, but one named -rf is even worse.

            – Monty Harder
            Mar 11 at 21:57













          8












          8








          8







          Unlike in some other operating systems, in Unixes, it's the shell that expands filename wildcards. It expands parameter expansions and globs first, then uses the (now) first word as the name for the command to run.



          This is also why files named with a leading dash can be troublesome: a glob like * will expand to the file names, and a name starting with a dash may be taken as an option. (The scary example being that a file called -r in the working directory would make rm * remove everything recursively...)



          Usually, one wouldn't use * as the first item on any command line, though, so having the first file determine the command to run in that case isn't likely to cause problems.






          share|improve this answer















          Unlike in some other operating systems, in Unixes, it's the shell that expands filename wildcards. It expands parameter expansions and globs first, then uses the (now) first word as the name for the command to run.



          This is also why files named with a leading dash can be troublesome: a glob like * will expand to the file names, and a name starting with a dash may be taken as an option. (The scary example being that a file called -r in the working directory would make rm * remove everything recursively...)



          Usually, one wouldn't use * as the first item on any command line, though, so having the first file determine the command to run in that case isn't likely to cause problems.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 11 at 22:33









          DarkHeart

          3,54632441




          3,54632441










          answered Mar 11 at 19:17









          ilkkachuilkkachu

          63.3k10104181




          63.3k10104181







          • 1





            I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

            – Michael Homer
            Mar 11 at 19:31











          • OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

            – osmak
            Mar 11 at 19:42











          • @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

            – ilkkachu
            Mar 11 at 19:56











          • @osmak That is a correct understanding.

            – Kusalananda
            Mar 11 at 19:56






          • 2





            A file named -r is bad, but one named -rf is even worse.

            – Monty Harder
            Mar 11 at 21:57












          • 1





            I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

            – Michael Homer
            Mar 11 at 19:31











          • OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

            – osmak
            Mar 11 at 19:42











          • @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

            – ilkkachu
            Mar 11 at 19:56











          • @osmak That is a correct understanding.

            – Kusalananda
            Mar 11 at 19:56






          • 2





            A file named -r is bad, but one named -rf is even worse.

            – Monty Harder
            Mar 11 at 21:57







          1




          1





          I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

          – Michael Homer
          Mar 11 at 19:31





          I think this example misses an important point out, that filename expansion is not subject to further field splitting, unlike parameter expansion, so filenames with spaces are still safe.

          – Michael Homer
          Mar 11 at 19:31













          OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

          – osmak
          Mar 11 at 19:42





          OK, so I think I misunderstood how the * work in the first place. I thought that it is a common syntax used in Linux commands. But, if I understood your answer correctly, then filename expansion is a shell feature not a command feature. So what happened is that * got replaced by all file or directory names in PWD but when the shell tried executing the first one it produced the error I saw.

          – osmak
          Mar 11 at 19:42













          @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

          – ilkkachu
          Mar 11 at 19:56





          @osmak, yeah, it's a shell feature on Unixes. Easier that way, so each and every program doesn't have to implement it. Though of course programs like find know how to expand similar patterns, too (e.g. file -name "*.txt")

          – ilkkachu
          Mar 11 at 19:56













          @osmak That is a correct understanding.

          – Kusalananda
          Mar 11 at 19:56





          @osmak That is a correct understanding.

          – Kusalananda
          Mar 11 at 19:56




          2




          2





          A file named -r is bad, but one named -rf is even worse.

          – Monty Harder
          Mar 11 at 21:57





          A file named -r is bad, but one named -rf is even worse.

          – Monty Harder
          Mar 11 at 21:57

















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505707%2fhow-is-the-wildcard-interpreted-as-a-command%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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?