Example:
I have alias chrome='google-chrome'
. I want which chrome
to return the same thing which google-chrome
returns, i.e.:
/usr/bin/google-chrome
Is that possible?
Don't use which
for this purpose, or better - don't use which
at all. Use type <alias_name>
to see alias expansion. Use type -a <command>
to see all possible categories that a given command can represent - programs from $PATH, shell builtins, functions, aliases. See type -a echo
for example.
EDIT:
You can also see alias expansion using alias <ALIAS_NAME>
form, for example:
$ alias chrome
alias chrome='google-chrome'
To only get a part after alias <ALIAS_NAME>='
you have parse the output like this:
$ alias chrome | sed "s,alias !#:1=',," | sed "s,'$,,"
Before doing that in a script make sure than alias <ALIAS_NAME>
returns zero.
chrome is an alias for google-chrome
which is exactly the point of my question (I want only google-chrome
).
– syntagma
Jan 25 '15 at 19:46
google-chrome
or the full path /usr/bin/google-chrome
as in the question. This the whole point of the problem.
– jimmij
Jan 25 '15 at 21:16
google-chrome
, but the full path would be OK.
– syntagma
Jan 25 '15 at 21:34
in zsh
, which
is a builtin
command that has the same effect of whence -c
. Which will print out how the each listed command would be interpreted in a csh
-like format.
In shells that doesn't have which
as a builtin. which
is ignorant of alias, builtins, functions and can only search PATH
for a command with the name.
An example is which echo
. which may print out /bin/echo
but echo is(normally) a builtin command also, which would have higher precedence than a binary in PATH
. Thus the shell would use the builtin and not /bin/echo
.
which
alias in some shells that call GNU which
after having fed it the list of known aliases.
– Stéphane Chazelas
Jan 25 '15 at 22:39
Should you really want which
to behave this way, you can redefine it as a shell function that way :
which() {
if [ -n "$(type "$1" | grep "is aliased")" ]; then
command which $(type "$1" | awk '
{cmd=gensub("[\140\047]", "", "g" , $NF);print cmd}')
else
command which "$1"
fi
}
Note that while this should work if your shell is bash
, the function might need to be slightly modified if you use a different shell.
If you want which chrome and which google-chrome to return the same response, create an alias for chrome (alias chrome=/usr/bin/google-chrome) and it will. Aliases are evaluated before commands, so this will change what the command which chrome returns.
-a
switch of which, it will give all possible outcomes, including aliased and the real one. – Ketan Jan 25 '15 at 18:47which
is very shell-dependent, so which shell we are talking about? – jimmij Jan 25 '15 at 19:03which
is really this script (when you are using bash): http://sources.debian.net/src/debianutils/4.4/which/ , so in short: then no. You can not get alias from which. – Runium Jan 25 '15 at 20:03