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?

8
  • Cory, please search the site before asking such a basic question. This question has been covered numerous times on the site. Commented Sep 10, 2013 at 22:40
  • Here's a good one: unix.stackexchange.com/questions/10525/… Commented Sep 10, 2013 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. Commented Sep 10, 2013 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!!!! Commented Sep 10, 2013 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-) Commented Sep 10, 2013 at 23:03

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.