Bypass aliases with the `command` command

April 26, 2016   

If you are writing a bash script to be used by others and you call external dependencies you might encounter a situation where a user has defined an alias in place of the original command.

For example users of the hub command line utillity may have an alias

alias git='hub'

Or something more practically challenging:

alias grep='grep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}'

If your command relies on the output of the original program without unexpected modifications you need a way to bypass the aliases and call the command directly.

To bypass aliases prefix the original program call it with the command command. For example:

command git --no-pager diff | command grep '[something]'

I like to think of it as the bash equivalent of Vim’s noremap.

About the Author
Responsive image

I'm Dorian Karter, a Full Stack Software Consultant.

I work at Hashrocket, a top web development consultancy based in Chicago.

Have a look at some of my open source projects.

Contact me at @dorian_escplan.