Is 'cat' a shell built-in or an external program?
type
tells you what the shell would use. For example:
$ type echo
echo is a shell builtin
$ type /bin/echo
/bin/echo is /bin/echo
That means that if, at the bash prompt, you type echo
, you will get the built-in. If you specify the path, as in /bin/echo
, you will get the external command.
which
, by contrast is an external program that has no special knowledge of what the shell will do. On debian-like systems, which
is a shell script which searches the PATH for the executable. Thus, it will give you the name of the external executable even if the shell would use a built-in.
If a command is only available as a built-in, which
will return nothing:
$ type help
help is a shell builtin
$ which help
$
Now, let;s look at cat
:
$ type cat
cat is hashed (/bin/cat)
$ which cat
/bin/cat
cat
is an external executable, not a shell builtin.
cat is hashed (/bin/cat)
is just like cat is /bin/cat
(that is, it's an external program).
The difference is that you already ran cat
in this session, so bash has already looked it up in $PATH
and stored the resulting location in a hash table so it doesn't have to look it up again in this session.
To see all the commands that have been hashed in your session, run hash
$ hash
hits command
2 /usr/bin/sleep
3 /usr/bin/man
$ type sleep
sleep is hashed (/usr/bin/sleep)
$ type man
man is hashed (/usr/bin/man)
$ type ls
ls is /usr/bin/ls
$ type cat
cat is /usr/bin/cat
$ type echo
echo is a shell builtin
You can also use the command whereis
that is more efficient because it shows where the command is on the machine like also the manual pages library, etc..