Is 'cat' a shell built-in or an external program?

10,015

Solution 1

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.

Solution 2

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

Solution 3

Another way to check list of shell builtin : Using compgen which is shell builtin itself!

Following command lists all shell builtin commands:

compgen -b

You can check for cat, echo by greping like:-

$ compgen -b | grep echo
echo
$ compgen -b | grep cat
$ 

You can see compgen -b | grep cat returns with no output, means cat is not shell builtin.

Visit a list of useful options provided by compgen.


You can also use one another builtin command : help to display shell-builtin.

$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.

Solution 4

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..

Solution 5

Since there are several good answers here about using type to find out if a command such as cat is a builtin or an external program. I am going to take a more general approach. There are some commands that must be builtins because they affect the current shell. Three classic examples are cd, exec, and exit. There are some commands that must not be builtins because their functionality depends on the behavior of the execve or system calls. Examples of such programs include su, sudo, calife and super. All other commands can be built as builtins or external programs. cat is a great example program of this class as there are shells that include it as a builtin and shells that do not. It is worth noting that many commands of this class that are available as builtins are also available as external programs.

Share:
10,015

Related videos on Youtube

sps
Author by

sps

Updated on September 18, 2022

Comments

  • sps
    sps almost 2 years

    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?

    • Joke Sr.  OK
      Joke Sr. OK about 9 years
      -The command cat is an external program that makes part of the system, per cause the many actions that it can perform is not a simple built-in.
    • mleonard
      mleonard about 9 years
      type which could give you the answer why which won't give you the answer.
    • nsn
      nsn about 9 years
      Depends on the shell you are using
    • Alec Teal
      Alec Teal about 9 years
      Fun fact: both cat and ls are/were written by Stallman himself. You'd be surprised how much stuff he wrote. Don't forget he wrote the first versions of the compiler that is today the best compiler in the world that compiles EVERYTHING and most of the core utils. Just sayin'
    • Ross Ridge
      Ross Ridge about 9 years
      @AlecTeal You do realize that most Unix systems use versions of ls and cat that long predate the GNU versions that Richard Stallman had a hand in?
    • Alec Teal
      Alec Teal about 9 years
      @RossRidge confirmed on Debian, if you get the source you'll notice the author is specified at the top of most files. That's my source, personal experience. So I stand by my coment
    • Ross Ridge
      Ross Ridge about 9 years
      @AlecTeal So you really do think Richard Stallman wrote the first version of cat and ls?
    • Alec Teal
      Alec Teal about 9 years
      @RossRidge not the first, nor the last. But wrote ones that are in use right now with every Debian (default) installation. For example I would imagine Solaros has its own
    • Nate Eldredge
      Nate Eldredge about 9 years
      @nsn: Do you actually know of any shells for which cat is a builtin? I don't.
    • felixphew
      felixphew about 9 years
      @AlecTeal FreeBSD uses its own cat and ls, for one. Most non-GNU/Linux systems would be the same.
    • Basile Starynkevitch
      Basile Starynkevitch about 9 years
      on sash the cat is a builtin.
    • nsn
      nsn about 9 years
      Another example would be ash + busybox. Not exactly embedded in the shell but busybox is a monolythic piece of software providing a very large set of shell commands (cat, ls, cp...) often used with ash.
    • Ross Ridge
      Ross Ridge about 9 years
      @NateEldredge Some versions of MKS shell have cat and ls as actual shell builtins. I think with busybox they show up as external commands even though they're all links to the same executable.
  • André Chalella
    André Chalella about 9 years
    Actually, in my opinion this is the only answer that addresses your question :)
  • Olivier Dulac
    Olivier Dulac about 9 years
    You could type -all echo to find out (in order) which are the different "echo" known to the shell (the first one being the one the shell will call, if you don't specify something to change the order, like invoking "echo" or \echo, or command echo )
  • Random832
    Random832 about 9 years
    I can't find any formal documentation for these options, do you know where it exists?
  • Joke Sr.  OK
    Joke Sr. OK about 9 years
    -Excuse me for my bad English, because I just learning yet and not so good using!...
  • Pandya
    Pandya about 9 years
    @Random832 are you talking about options for compgen?
  • Random832
    Random832 about 9 years
    yeah, I couldn't find anything in the Bash manpage about what -b or half the other options mean. Found it later at gnu.org/software/bash/manual/html_node/…
  • Pandya
    Pandya about 9 years
    @Random832 try man bash | grep -e '-A action$' -A 32 you probably get output like this. (increase/decrease digit after -A to manage properly).
  • alexis
    alexis about 9 years
    Good answer. One addition: There are two versions of echo for historical reasons. It started out as an external command, then was added as a built-in. Early versions of the Bourne shell (/bin/sh) did not have it. /bin/echo was kept on for compatibility, since all sorts of things depended on it. (The same thing happened with test.)
  • Dan Cornilescu
    Dan Cornilescu about 9 years
    Side note: which itself can be a shell built-in cmd, for example in tcsh: which which which: shell built-in command.
  • poz2k4444
    poz2k4444 about 9 years
    I'm arriving late to the party, but why don't you check its man page and you can actually see the number between parenthesis, which indicates if it is a shell built-in or whatever
  • Vlastimil Burián
    Vlastimil Burián over 6 years
    IMHO this ^^^ is true. This answer should be the accepted one.