You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
stuff-and-nonsense edited this page May 12, 2019
·
14 revisions
which is non-standard. Use builtin 'command -v' instead.
Problematic code:
which grep
Correct code:
command -v grep
Rationale:
which is a non-standard, external tool that locates an executable in PATH. command -v is a POSIX standard builtin, which uses the same lookup mechanism that the shell itself would.
Exceptions:
None
Caveats:
With BASH 5.0.7 (via homebrew on macOS 10.13.6), command -v appears to take multiple parameters:
# grep is in /usr/bin/grep
# foobar is not in path
#
$ command -v -- grep foobar; echo $?
0
but succeeds (with exit code 0) if any command exists. In the above
example, it should have failed and exited with 1 unless all commands
exist.
An alternative is:
$ hash <file1> <file2>
Which observes the standard behaviour of failures.