I wanted to find the read command, so I did:
$ which read
It returns exit status 1. Why does this happen?
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?
cd, read is always a builtin because it modifies shell internal state, so it couldn't be implemented as a separate program.
– Simon Richter
Jul 02 '19 at 10:24
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.
which read prints no message. It appears you're on a system whose which executable is provided by GNU which. But /bin/which in Ubuntu is from debianutils. Some shells have a which builtin but bash doesn't. The behavior of the external command which varies greatly by OS, including across GNU/Linux distros. This is one of several reasons to prefer type or command -v. (On some OSes, which is even a csh script.)
– Eliah Kagan
Jul 02 '19 at 18:57
read program should work.
– U. Windl
Jul 04 '19 at 09:19
readis abashbuiltin, not a command on its own. Find information aboutreadinman bash( https://manpages.ubuntu.com/manpages/bionic/en/man1/bash.1.html ) – waltinator Jul 01 '19 at 18:00readis built into bash. There is no exectuable for that command. – PerlDuck Jul 01 '19 at 18:01read, you can run the commandhelp read. See the this link for more details – sudodus Jul 01 '19 at 18:17echois a built-in command, but it has an executable path. @waltinator @PerlDuck – Mohammad Kholghi Jul 01 '19 at 18:36bash'sbuiltin echois NOT the same as/bin/echo, which supports ESCape sequences. @PerlDuck – waltinator Jul 01 '19 at 22:01echobuiltin isn't the same as the external command/bin/echothat, in Ubuntu, is provided by GNU coreutils. But both support escape sequences, and both interpret them when the-eoption is passed and not otherwise. In general, on an arbitrary system with an arbitrary Bourne-style shell, one cannot be sure itsechobuiltin (if any!) and the external command (which is required to be present) behave in a largely similar fashion. But with bash and/bin/echoin Ubuntu and most other GNU/Linux systems, they do. – Eliah Kagan Jul 02 '19 at 00:40