what is bash command `command` used for and what is it when it appears in shell script











up vote
1
down vote

favorite












when I was writing a shell script, I happen to know command is a reserved word. Then I noticed command is also a bash command.





  • command ls gives the same result as ls.


  • command ls | grep xxx gives the same result as without it.


Then I wonder what is it used for when it is a bash command. And what is it used for when it is in shell script. Thanks!










share|improve this question


























    up vote
    1
    down vote

    favorite












    when I was writing a shell script, I happen to know command is a reserved word. Then I noticed command is also a bash command.





    • command ls gives the same result as ls.


    • command ls | grep xxx gives the same result as without it.


    Then I wonder what is it used for when it is a bash command. And what is it used for when it is in shell script. Thanks!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      when I was writing a shell script, I happen to know command is a reserved word. Then I noticed command is also a bash command.





      • command ls gives the same result as ls.


      • command ls | grep xxx gives the same result as without it.


      Then I wonder what is it used for when it is a bash command. And what is it used for when it is in shell script. Thanks!










      share|improve this question













      when I was writing a shell script, I happen to know command is a reserved word. Then I noticed command is also a bash command.





      • command ls gives the same result as ls.


      • command ls | grep xxx gives the same result as without it.


      Then I wonder what is it used for when it is a bash command. And what is it used for when it is in shell script. Thanks!







      bash shell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 at 8:46









      Tiina

      5711514




      5711514






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          command foo will run the foo command even if there is a foo shell function defined. This behavior is required by POSIX.



          It allows you to call the foo command inside the foo function. Without command foo the function (when invoked) would call itself and create a circular reference.



          Well, sometimes you can call the foo executable by its full path to suppress the shell function lookup (cumbersome and not portable, still some kind of workaround), but in some cases you can't. E.g. in this answer of mine (near the end) I redefine cd and command cd is a must, because even if there is a cd executable which full path I could use, it wouldn't change the directory. Using command is the right way to deal with this.



          Also note command foo will not trigger a foo alias (if any). You used ls as an example. At least few common Linux distros alias ls to ls --color=auto by default. In this case ls and command ls may give different results (i.e. colored or not). POSIX (or any other) definition of command doesn't need to mention aliases because bar foo doesn't trigger foo alias (if any) in the first place (with few possible exceptions regarding bar, but command is not one of them).



          Whether you use command foo in an interactive shell session or in a shell script may matter to foo, but not to the command builtin itself.






          share|improve this answer























            Your Answer








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


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1376181%2fwhat-is-bash-command-command-used-for-and-what-is-it-when-it-appears-in-shell%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








            up vote
            2
            down vote



            accepted










            command foo will run the foo command even if there is a foo shell function defined. This behavior is required by POSIX.



            It allows you to call the foo command inside the foo function. Without command foo the function (when invoked) would call itself and create a circular reference.



            Well, sometimes you can call the foo executable by its full path to suppress the shell function lookup (cumbersome and not portable, still some kind of workaround), but in some cases you can't. E.g. in this answer of mine (near the end) I redefine cd and command cd is a must, because even if there is a cd executable which full path I could use, it wouldn't change the directory. Using command is the right way to deal with this.



            Also note command foo will not trigger a foo alias (if any). You used ls as an example. At least few common Linux distros alias ls to ls --color=auto by default. In this case ls and command ls may give different results (i.e. colored or not). POSIX (or any other) definition of command doesn't need to mention aliases because bar foo doesn't trigger foo alias (if any) in the first place (with few possible exceptions regarding bar, but command is not one of them).



            Whether you use command foo in an interactive shell session or in a shell script may matter to foo, but not to the command builtin itself.






            share|improve this answer



























              up vote
              2
              down vote



              accepted










              command foo will run the foo command even if there is a foo shell function defined. This behavior is required by POSIX.



              It allows you to call the foo command inside the foo function. Without command foo the function (when invoked) would call itself and create a circular reference.



              Well, sometimes you can call the foo executable by its full path to suppress the shell function lookup (cumbersome and not portable, still some kind of workaround), but in some cases you can't. E.g. in this answer of mine (near the end) I redefine cd and command cd is a must, because even if there is a cd executable which full path I could use, it wouldn't change the directory. Using command is the right way to deal with this.



              Also note command foo will not trigger a foo alias (if any). You used ls as an example. At least few common Linux distros alias ls to ls --color=auto by default. In this case ls and command ls may give different results (i.e. colored or not). POSIX (or any other) definition of command doesn't need to mention aliases because bar foo doesn't trigger foo alias (if any) in the first place (with few possible exceptions regarding bar, but command is not one of them).



              Whether you use command foo in an interactive shell session or in a shell script may matter to foo, but not to the command builtin itself.






              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                command foo will run the foo command even if there is a foo shell function defined. This behavior is required by POSIX.



                It allows you to call the foo command inside the foo function. Without command foo the function (when invoked) would call itself and create a circular reference.



                Well, sometimes you can call the foo executable by its full path to suppress the shell function lookup (cumbersome and not portable, still some kind of workaround), but in some cases you can't. E.g. in this answer of mine (near the end) I redefine cd and command cd is a must, because even if there is a cd executable which full path I could use, it wouldn't change the directory. Using command is the right way to deal with this.



                Also note command foo will not trigger a foo alias (if any). You used ls as an example. At least few common Linux distros alias ls to ls --color=auto by default. In this case ls and command ls may give different results (i.e. colored or not). POSIX (or any other) definition of command doesn't need to mention aliases because bar foo doesn't trigger foo alias (if any) in the first place (with few possible exceptions regarding bar, but command is not one of them).



                Whether you use command foo in an interactive shell session or in a shell script may matter to foo, but not to the command builtin itself.






                share|improve this answer














                command foo will run the foo command even if there is a foo shell function defined. This behavior is required by POSIX.



                It allows you to call the foo command inside the foo function. Without command foo the function (when invoked) would call itself and create a circular reference.



                Well, sometimes you can call the foo executable by its full path to suppress the shell function lookup (cumbersome and not portable, still some kind of workaround), but in some cases you can't. E.g. in this answer of mine (near the end) I redefine cd and command cd is a must, because even if there is a cd executable which full path I could use, it wouldn't change the directory. Using command is the right way to deal with this.



                Also note command foo will not trigger a foo alias (if any). You used ls as an example. At least few common Linux distros alias ls to ls --color=auto by default. In this case ls and command ls may give different results (i.e. colored or not). POSIX (or any other) definition of command doesn't need to mention aliases because bar foo doesn't trigger foo alias (if any) in the first place (with few possible exceptions regarding bar, but command is not one of them).



                Whether you use command foo in an interactive shell session or in a shell script may matter to foo, but not to the command builtin itself.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 17 at 10:03

























                answered Nov 17 at 9:05









                Kamil Maciorowski

                22.7k155072




                22.7k155072






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1376181%2fwhat-is-bash-command-command-used-for-and-what-is-it-when-it-appears-in-shell%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

                    AnyDesk - Fatal Program Failure

                    How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

                    QoS: MAC-Priority for clients behind a repeater