55abhilash
55abhilash

Reputation: 332

Use of the 'command' built-in in Bash shell

What is the use of the 'command' bash shell built in? This page says that it suppresses the shell function lookup, but I am not sure what it means. Can you explain or give an example?

Upvotes: 1

Views: 249

Answers (2)

John1024
John1024

Reputation: 113834

Observe:

$ date() { echo "This is not the date"; }
$ date
This is not the date
$ command date
Tue Aug  2 23:54:37 PDT 2016

Upvotes: 5

cdarke
cdarke

Reputation: 44354

Lets take a simple example of a function. We want to make sure that cp always uses the -i option. We could do that using an alias, but aliases are simple and you can't build much intelligence into them. Functions are much more powerful.

We might try this (remember, this is just a simple example):

cp() {
    cp -i "$@"
}

cp gash.txt gash2.txt

That gives us infinite recursion! It just keeps calling itself. We could use /bin/cp in this case, but this is what command is for:

cp() {
    command cp -i "$@"
}

cp gash.txt gash2.txt

That works, because now it ignores the cp function.

Upvotes: 1

Related Questions