1

This question already has an answer here:

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?

marked as duplicate by Gilles, jordanm, jasonwryan, Hauke Laging, jimmij Jan 26 '15 at 1:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • you can use the -a switch of which, it will give all possible outcomes, including aliased and the real one. – mkc Jan 25 '15 at 18:47
  • @Ketan The -a switch does not work that way. – syntagma Jan 25 '15 at 18:48
  • which is very shell-dependent, so which shell we are talking about? – jimmij Jan 25 '15 at 19:03
  • Bash, though I am using zsh as well. – syntagma Jan 25 '15 at 19:05
  • 1
    Then your which is really this script (when you are using bash): 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
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.

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.

  • Well, in my case it prints: 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
  • @REACHUS do you want 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
  • @jimmij Preferably just google-chrome, but the full path would be OK. – syntagma Jan 25 '15 at 21:34
  • @REACHUS: see updated answer – Arkadiusz Drabczyk Jan 25 '15 at 21:49
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.

  • Beware that some systems have a default 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
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.