Why I use shell aliases sparingly

by js on

There comes a point in every sysadmin’s (early) life when he learns about shell aliases: you can assign a special nickname or alias to any command, including ones with switches, pipes, redirections, operators, multilines– anything you could execute in the shell or in a script. It can save time when typing a commonly used but long or complicated command, so aliases come in handy, speeding up many sysadmin tasks.

For example, I could condense the following:

tail -200f /var/log/nginx/access_log

to:

tailf-access

and then instead of typing 36 characters 54 times a day during the course of my sysadminneries, I only need to type 12 characters 54 times a day: a whopping 66% savings in this case.

However, aliases can easily get out of hand. If you own 20,000 servers as is not uncommon in the enterprise and want to be able to use your special alias on any of them to which you might end up SSHing at any time, you must have some kind of management system to push these aliases out to the user or system bash profile. Such management software also isn’t unusual in such an enterprise, but unless you’re in charge of said software, you’re at the mercy of whomever is to give you some good aliases, or any at all. Hopefully they listen to you better than most ITkin listen to anybody so you have a shot of getting what you want (as an IT person of any flavor, you just need to accept that a lot of your problems are probably caused by either your terrible communication, the terrible communication of those you work with, or most likely both).

Then when you go to manage different machines at home or on a side job or if you move to another company or another department of your own company that doesn’t follow the same rules, you may have completely different aliases, or none at all, or– worst of all– some homonymic aliases that have different behaviors than what you’re used to.

Thus I tend to use aliases in very specific cases– mostly on my local machine for specific, niche commands such as for connecting to some VPNs, generating some certain tokens, or even converting Unix epoch timestamps.

In many cases it takes a sysadmin just as long to remember a custom alias to tail a special log as it does to simply tail that log manually. The time savings many argue are significant over the course of executing the same command dozens of times per day, but my argument against it comes in two components:

  • The mental cost when switching between ecosystems containing different aliases (or none at all) is perhaps not immense but is also not insignificant. Perhaps muscle memory can be more efficient or useful than aliases. Perhaps on the rare occasion pursuing the right course demands an act of piracy, piracy itself can be the right course.
  • The possibility of command-knowledge atrophy is always present, especially when one doesn’t ever use the real commands themselves. Becoming a pro at those specific switches may be more beneficial than saving a few seconds. Eventually you’ll know them so well that you won’t even save a few seconds with an alias, and you’ll win friends and influence demonstrating your prowess, even finding more creative ways to pipe certain commands together to solve a problem that you otherwise wouldn’t have figured out had you taken the easy route of relying on aliases all the time.

This is why I am very particular about the few aliases I use and where I use them.

Outside of the enterprise management tools, some people carry around their own aliases with them to whatever systems they log in to like a hobo carrying a knapsack. Some even carry around shell prompt colors and configuration, sort of like customizing your wallpaper, UI colors, and system sounds back when Windows 95 was cool. This all seems to work out fine for them. And in the case of prompt configurations, that can end up being pretty handy to know the host, system class, environment class, and other bits of configurable info about a system just at a glance. I’m not knocking the idea, so don’t get your shellcode in a bunch.

Others just roll with the punches and like having different aliases in different ecosystems and take the time savings for what they’re worth.

I don’t. I prefer the life of a nomad– encouraged– forced, even– to rely on hard-won knowledge, wit, and fortitude to ensure I can face any challenge on any system, not being tied down mentally to constructs that don’t exist out in the wild and that can’t always be guaranteed to exist even within the safe spaces of your walled garden.

This isn’t meant to be some kind of evangelical rant. If you use a thousand different aliases on a single system and find it helps your workflow more than my philosophy ever could, more power to you. If you use a handful of specific aliases for specific types of instances you manage across different ecosystems and it works for you, fantastic. Different strokes and all that.

All I ask is that the next time we’re screensharing in a common ecosystem where we share the same aliases and you see me type out a full, raw, gruesome, bloody command to do some common task and you are tempted to say “you can just use such-and-such alias”, remember my reasons for not participating in this phenomenon and be like Paul McCartney: let it be.