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 lsgives the same result asls.
command ls | grep xxxgives 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
add a comment |
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 lsgives the same result asls.
command ls | grep xxxgives 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
add a comment |
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 lsgives the same result asls.
command ls | grep xxxgives 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
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 lsgives the same result asls.
command ls | grep xxxgives 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
bash shell
asked Nov 17 at 8:46
Tiina
5711514
5711514
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 17 at 10:03
answered Nov 17 at 9:05
Kamil Maciorowski
22.7k155072
22.7k155072
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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