Find where a bash command is defined or sourced from?

5,357

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.

Share:
5,357

Related videos on Youtube

Cory Klein
Author by

Cory Klein

Updated on September 18, 2022

Comments

  • Cory Klein
    Cory Klein over 1 year

    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?

    • Admin
      Admin over 10 years
      Cory, please search the site before asking such a basic question. This question has been covered numerous times on the site.
    • Admin
      Admin over 10 years
    • Admin
      Admin over 10 years
      @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.
    • Admin
      Admin over 10 years
      @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!!!!
    • Admin
      Admin over 10 years
      @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-)
    • Admin
      Admin over 10 years
      @CoryKlein - absolutely, duplicates are rarely if ever deleted!!!
    • Admin
      Admin over 10 years
      @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.
    • Admin
      Admin over 10 years
      @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!