type -a

October 23, 2014

I mentioned previously that which -a is what you want to use to find what command you’re about to run.

I was wrong.

I found accidentally, probably while reading something on stackoverflow, that type -a is a better-in-every-way replacement for which -a. Compare:

% which -a ls
/Users/jpalardy/local/bin/ls
/bin/ls

% type -a ls
ls is aliased to 'ls --color=auto'
ls is /Users/jpalardy/local/bin/ls
ls is /bin/ls

Not only does type -a give me all the same information, it also tells me about aliases and functions. Consider what happens for functions which do not have corresponding executables in the PATH:

% which -a gril
                                #   -- no output --

% type -a gril
gril is a function
gril ()
{
  grep -il "$@"
}

It tells me it’s a function and it shows me its definition. This has decreased, subtantially, the amount of grepping my dotfiles trying to remember how certain functions work.

Discussion, links, and tweets

Follow me on Twitter