Where is the read command?
I wanted to find the read
command, so I did:
$ which read
It returns exit status 1. Why does this happen?
read
is a shell builtin, not an external command. which
only tells you about external commands. Assuming you're using Bash (or some other Bourne-style shell), you should typically use type
or command -v
instead of which
.
ek@Cord:~$ type read
read is a shell builtin
type
and command
are themselves shell builtins and they know not just about external commands but also about keywords, builtins, aliases, and functions. which
is an external command that doesn't know about those things; it only knows about external commands. Sometimes which
doesn't turn up anything when you ask it about a command that you can use in your shell. Sometimes it does turn up something for a command, but it isn't the same thing that actually runs when you use the command in your shell.
ek@Cord:~$ type type command which
type is a shell builtin
command is a shell builtin
which is /usr/bin/which
In Bash, you can see all the current possible meanings for a command, in the order that they are tried, with type -a
:
ek@Cord:~$ type -a read
read is a shell builtin
ek@Cord:~$ type -a echo
echo is a shell builtin
echo is /bin/echo
For more information about why you usually shouldn't use which
, and what to use instead in various shells including Bash, see Why not use “which”? What to use then?
If you know a little how UNIX and the shell work, it should be obvious from the syntax (read var1 var2 ...
) that no external program can set a local shell variable, so read
must be built into the shell.
In bash you do not just get an exit code of 1, but also a message like which: no read in (/home/user/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games)
. That should ring a bell that if read
is still found, it must be built-in to the shell.