Why "alias" is my last resort for aliases

by
, updated (originally posted )

Aliases were one of the first things I added when customizing my dotfiles. For example, here’s a very early alias I defined:

alias g=git

Now, I can run g instead of git, which saves me a little time for a command I run dozens of times a day!

# These two commands are now equivalent:
git status
g status

I used to define these aliases with alias. After all…I’m defining an alias!

But over time, I think I discovered a better way: a script in my $PATH.

How it works

In my home directory, I have a folder of scripts called bin. For example, here’s a simplified version of ~/bin/g:

#!/usr/bin/env bash
exec git "$@"

Running this script basically just runs git.

I add this folder to my $PATH. (See Julia Evans’s guide on how to do this.) In my .zshrc, I have a line like this:

export PATH="$HOME/bin:$PATH"

Now, when I type g, it runs that script.

This behaves just like an alias. As before, g status and git status are equivalent.

# These two commands are still the same:
git status
g status

This is a lot more verbose than alias. So why do it this way?

Benefits of scripts over aliases

Scripts have several advantages over using alias:

These benefits are enough to convince to use scripts as my default, even for simple aliases like g=git.

Benefits of aliases over scripts

Scripts are my preference but they aren’t perfect. Everything in programming has tradeoffs!

There are a few things that are better about alias:

Choose your favorite

Ultimately, this decision doesn’t make much difference. Both methods—aliases and scripts—are pretty similar. But for me, I default to using scripts because I like what they can do.