Developer Hacks - Aliases in Bash cover image

Developer Hacks - Aliases in Bash

Oliver Sarfas • February 27, 2019

programming

If you've seen my YouTube series on building a Twitter Clone, which I also stream on Twitch.tv, then you know that I use commands that are not "native" to the bash command line.

To do this, I setup aliases, this is to make my life easier, and I hope to make my development process faster in the process. But what are aliases? How do you write them? What about making them load at boot / opening of your terminal..? Well, here's my setup, and a little package you can download to get yourself started 👍

What's an alias?

An alias is basically a shortcut command that you can declare. It's designed to save you input time. The simplest one that I use day-to-day is c. When I execute this, my bash actually runs the clear command. It's only 4 characters, but that's a lot of time saved, as I run this at least 20-30 times a day.

How do I create one?

alias {term}="{command to run}"

That's it. Single line command.

seth meyers boom GIF by Late Night with Seth Meyers

What more could you want?

However, this alias will not persist. Once you close your terminal, you lose the command. Not very helpful if you're often jumping in and out of terminals all day.

How do I keep my aliases?

Now you're talking! Personally, I version control all my aliases, and then load them in. This means that no matter what environment I'm in, I've always got my aliases with me 👍

To load your aliases into every terminal (for your given environment), you will have to create, or edit, a .bash_profile file. This can be found in your $HOME directory, also known as ~. The easiest way to do this, is run the command, vi ~/.bash_profile 👍

The contents of this file should simply list your aliases, so, if you want the c command I mentioned earlier, your file will have a line with alias c="clear" in it. Simples.

For easier management, I prefer to have all my aliases in a single file, called aliases. In your bash profile, you can then call source aliases. This loads ALL the file into your bash profile for you; and you can manage them from a single file.

Can I use your aliases?

Of course. I host my "dotfiles" repository on GitHub, found here. Usage is pretty simple, and the readme should help all users get this working. If you're struggling, drop a comment below and I'll be happy to help!

Questions? Want to talk? Here are all my social channels