There's no real difference in behavior. Both commands do nothing and exit with a successful status. :
emphasizes doing nothing; true
emphasizes the successful status.
strace true
works because true
is both a shell builtin and an external command (/bin/true
); :
is only a shell builtin (there's no /bin/:
-- though there could be, and probably was on very old Unix systems). In bash, try
type -a :
type -a true
The reasons that both exist are historical. If I recall correctly, some very early shells didn't have a comment syntax, so the do-nothing :
command was used instead.
There is some internal difference in dash
. Looking through the source, available at git://git.kernel.org/pub/scm/utils/dash/dash.git, shows some different code paths in eval.c
, but I haven't been able to produce any visibly different behavior other than the word special
in the output of type :
.
zsh
is one of the few shells (the other ones being tcsh
(which
originated as a csh
script for csh
users, which also had its limitation, tcsh
made it a builtin as an improvement)) where which
does something sensible since it's a shell builtin, but somehow you or your OS (via some rc
file) broke it by replacing it with a call to the system which
command which can't do anything sensible reliably since it doesn't have access to the interns of the shell so can't know how that shell interprets a command name.
In zsh, all of which
, type
, whence
and where
are builtin commands that are all used to find out about what commands are, but with different outputs. They're all there for historical reason, you can get all of their behaviours with different flags to the whence
command.
You can get the details of what each does by running:
info -f zsh --index-search=which
Or type info zsh
, then bring up the index with i
, and enter the builtin name (completion is available).
And avoid using /usr/bin/which
. There's no shell nowadays where that which
is needed. As Timothy says, use the builtin that your shell provides for that. Most POSIX shells will have the type
command, and you can use command -v
to only get the path of a command (though both type
and command -v
are optional in POSIX (but not Unix, and not any longer in LSB), they are available in most if not all the Bourne-like shells you're likely to ever come across).
(BTW, it looks like /usr/bin
appears twice in your $PATH
, you could add a typeset -U path
to your ~/.zshrc
)
Best Answer
From your comments, you seem to be confused about exactly what a shell is. The kernel is responsible for managing the system. It's the part that actually loads and runs programs, accesses files, allocates memory, etc. But the kernel has no user interface; you can only communicate with it by using another program as an intermediary.
A shell is a program that prints a prompt, reads a line of input from you, and then interprets it as one or more commands to manipulate files or run other programs. Before the invention of the GUI, the shell was the primary user interface of an OS. On MS-DOS, the shell was called
command.com
and few people ever tried to use a different one. On Unix, however, there have long been multiple shells that users could pick from.They can be divided into 3 types. The Bourne-compatible shells use the syntax derived from the original Bourne shell. C shells use the syntax from the original C shell. Then there are nontraditional shells that invent their own syntax, or borrow one from some programming language, and are generally much less popular than the first two types.
A built-in command is simply a command that the shell carries out itself, instead of interpreting it as a request to load and run some other program. This has two main effects. First, it's usually faster, because loading and running a program takes time. Of course, the longer the command takes to run, the less significant the load time is compared to the overall run time (because the load time is fairly constant).
Secondly, a built-in command can affect the internal state of the shell. That's why commands like
cd
must be built-in, because an external program can't change the current directory of the shell. Other commands, likeecho
, might be built-in for efficiency, but there's no intrinsic reason they can't be external commands.Which commands are built-in depends on the shell that you're using. You'll have to consult its documentation for a list (e.g.,
bash
's built-in commands are listed in Chapter 4 of its manual). Thetype
command can tell you if a command is built-in (if your shell is POSIX-compatible), because POSIX requires thattype
be a built-in. Ifwhich
is not a built-in in your shell, then it probably won't know about your shell's built-ins, but will just look for external programs.