Bash – Is ‘cat’ a shell built-in or an external program

bashcatechoshell-builtin

When I use the type command to find out if cat is a shell built-in or an external program I get the output below:

-$ type cat
cat is hashed (/bin/cat)
-$

Does this mean that cat is an external program which is /bin/cat?

I got confused, because when I checked the output below for echo I got to see that it is a built-in but also a program /bin/echo

-$ type echo
echo is a shell builtin
-$ which echo
/bin/echo
-$ 

So I could not use the logic that /bin/cat necessarily means an external program, because echo was /bin/echo but still a built-in.

So how do I know what cat is? Built-in or external?

Best Answer

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.