SDB:Which command

From openSUSE


Contents

Situation

A command you type in on the commandline doesn't seem to match the one documented by the man command. You need to figure out which command is actually being run.

Procedure

For instance, in the bash shell, you type:

$ time -o time.out ls
bash: -o: command not found

But the man page shows a -o option. Which time command is being run? A couple of different commands can come to your rescue.

Command: type

The bash builtin type command is the way to go. It includes aliases and builtins when it searches for its target.

$ type time
time is a shell keyword
$ type -a time
time is a shell keyword
time is /usr/bin/time

Use the help builtin to get more information on shell keywords.

$ help time
time: time [-p] PIPELINE
    Execute PIPELINE and print a summary of the real time, user CPU time,
   and system CPU time spent executing PIPELINE when it terminates.
   The return status is the return status of PIPELINE.  The `-p' option
   prints the timing summary in a slightly different format.  This uses
   the value of the TIMEFORMAT variable as the output format.
times: times
    Print the accumulated user and system times for processes run from
   the shell.

You'll see that this time command doesn't include the -o option. So to use the man documented version, you'll need to type in the full path to it.

$ /usr/bin/time -o time.out ls

Command: which

The which command will list where it finds the command found in the PATH. The -a option is of particular interest, as it will list all instances it finds on the PATH, not just the first one. However, it does not know about shell aliases or builtins.

$ which time
/usr/bin/time

Also note that under a standard 10.2 install, which is aliased in /etc/bash.bashrc to be a shell function and not the one found in /usr/bin:

$ type -a which
which is aliased to `_which'
which is /usr/bin/which
$ type -a _which
_which is a function
_which () 
{ 
   local file=$(type -p ${1+"$@"} 2>/dev/null);
   if test -n "$file" -a -x "$file"; then
       echo "$file";
       return 0;
   fi;
   hash -r;
   type -P ${1+"$@"}
}

Links

Daemon Dancing in the Dark blog - Does anybody really know what time it is?