Use the bash type
builtin or command -v
, not which
, to determine what your shell will actually do when a command is run. (Don't use the output of any of these to form another command in the way your question suggests; the code given in your question would fail if the output for which
contained a directory name with a space, a glob literal prone to expansion, or similar).
which
tells you what's first in your PATH, but since it's an external tool rather than part of the shell, it's unaware of behaviors that depend on the shell's state. (Think aliases, functions, hashed lookups, etc).
If you have an alias, you may see something like the following:
$ type wine
wine is aliased to `/Applications/Wine.app/Contents/Resources/bin/wine'
$ command -v wine
alias wine='/Applications/Wine.app/Contents/Resources/bin/wine'
...or, for a function:
$ type wine
wine is a function
wine ()
{
/Applications/Wine.app/Contents/Resources/bin/wine "$@"
}
$ command -v wine
wine