7

Sometimes I want to know what a command I type into bash actually evaluates to. Usually I can figure out the location of the executable very easily with which.

$ which vim
/usr/bin/vim

But what if I've created an alias for vim?

$ alias vim="echo mwahaha"
$ which vim
/usr/bin/vim

Hmm, not good. If I suspect something and want to check if vim has been aliased, I know I can type

alias vim

... but if I had an alias vim='vim -p', I would never actually think to check vim aliases because it would still work just fine.

How can I know exactly what a specific command in bash is being evaluated to?

Cory Klein
  • 18,911
  • Cory, please search the site before asking such a basic question. This question has been covered numerous times on the site. – slm Sep 10 '13 at 22:40
  • Here's a good one: http://unix.stackexchange.com/questions/10525/how-to-use-which-on-an-aliased-command/10532#10532 – slm Sep 10 '13 at 22:42
  • @slm I was actually positive that there was already such a question on the site, but my searches didn't yield such a question - most likely because of my wording. I agree with meta though, that if my search doesn't result in an answer to my question that the question can still be asked, and that these questions improve the site. I also agree that this is a duplicate and should be closed - just not deleted. – Cory Klein Sep 10 '13 at 22:58
  • @CoryKlein - I 100% agree with you. I've often done extensive searches and not found anything relevant only to have someone else put a duplicate (usually Gilles) on something that I was convinced had none. Duplicates serve an important function to the SE sites in providing paths to the penultimate Q&A's but on something simple as this, I would always bet on the house that there is already a Q&A on it. Just my $0.02's obviously, we always appreciate questions, so it's best to error on the side of asking!!!! – slm Sep 10 '13 at 23:03
  • @CoryKlein - also my comment was 1/2 directed to Chris who in answering this should've known that this was a duplicate and directed you 8-) – slm Sep 10 '13 at 23:03
  • @CoryKlein - absolutely, duplicates are rarely if ever deleted!!! – slm Sep 10 '13 at 23:05
  • @slm Unless I know for sure it's a duplicate and can link to it without much work searching around, I don't go looking. I don't feel like that's something I'm required to do as an answerer, and I don't feel like it's a good use of my time considering the amount of questions I read. Such prerequesites for answering a question are a good way to induce burnout in top answerers. It's the responsibility of the person asking the question to make sure it isn't a duplicate, it's not the responsibility of the person answering it. – Chris Down Sep 10 '13 at 23:19
  • @ChrisDown - I said 1/2 8-). I completely understand your side as well, as an answerer you're goal is to provide a solution, nothing more, if you know it then answer it, if you remember seeing it before then direct them to it. If not others will ultimately mark it as a dup if it truly is. There is no right or wrong here, you are generously providing others with your time and you do as you wish. I was merely poking you since you're a high rep guy on this site and you know the drill 8-). I +1'd you cuz your answer is right! – slm Sep 10 '13 at 23:24

1 Answers1

13

Use type, which is internal to bash.

$ type vim
vim is /usr/bin/vim
$ type -p vim
/usr/bin/vim
$ alias vim="echo mwahaha"
$ type vim
vim is aliased to `echo mwahaha'
$ type -p vim
$

There's a good breakdown of the different ways to get information about a command in this answer by Stephane Chazelas. You shouldn't rely on which, even non-maliciously, it doesn't know about your shell's hash lookup table, which can cause problems.

Chris Down
  • 125,559
  • 25
  • 270
  • 266