2

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?

syntagma
  • 12,311

4 Answers4

1

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.

1

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.

llua
  • 6,900
1

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.

jlliagre
  • 61,204
0

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.

Sandra H-S
  • 21
  • 1