Skip to content

How and Why to Make a BASH Alias

This Quick Guide covers a common question whose lingo may confuse newbies: how do I make a Bash shell alias? For those for whom that sentence was Greek—let start here: Bash is the “Bourne Again Shell.” (There’s not relationship to the Matt Damon movie character, for better or worse. ;p) In the world of “command line interfaces,” Bash has been the go-to standard for decades, and is still the most common “terminal” in use. In the “terminal” world, Bash aliases solve a common problem: wanting to do things quicker and more effectively. So we’ll quickly and effectively walk you through how to make BASH aliases.

Video Summary of Making BASH Aliases

One of the most obviously annoying things about the terminal — especially to those of us living in the age of touch and GUIs — is how much you need to type. Typing `ssh user@1.1.1.1` is never going to feel as fast as hitting a “connect” button, for example. As such, some people just never really like the terminal. While it’s hardly a solution to all your complaints, BASH (and most other Unix terminals) offer a solution to the issue of typing long commands. They’re called “aliases.” They’re essentially shortcuts to commonly typed sequences that can be as short as one letter. In the case of that’s video that’s exactly what we’ve done.

In this video, I alias (the already pretty short :p) command `clear` to be executable with the letter `c`. This can serve as a template for you to make lots of other relevant aliases that are more specific to your work. Here’s the video:

And the text explanation, if that’s more your speed:

How to Create a Bash Alias That Persists Over Time

Simply creating a BASH alias is pretty simple. It just requires executing a simple command: alias c="clear". If you run that in your terminal, you’ll have a working alias that means that typing “c” will clear your terminal window. The issue is, once you end that terminal session, that alias will go away. For that reason, you typically actually use a more complex feature of BASH when you’re making aliases. You use the .bashprofile or .bashrc file to store those alias commands and have them rerun each time your terminal restarts. BASH will look for and run both of those files before it opens, so your aliases will always be there.

Here’s how you create persistent aliases in your terminal:

  1. Open your terminal emulator of choice. I favor iTerm 2 on the Mac. Macs and most Linux distributions come with one called “Terminal”. (Note: on Macs “zsh” has been the default shell since macOS Catalina (10.15). Here’s a useful guide for understanding differences between zsh and Bash with regard to aliases. Short version: where I refer to .bashrc, you’ll want to use .zshrc, the rest is the same.)
  2. Type sudo nano ~/.bashrc in your BASH terminal window. This will open up the Nano text editor, which is my personal favorite for simple editing. You can use others. sudo is likely to be necessary for permission reasons, if it is and you use it, supply the password before going on to the next step.
  3. In that file, add the line: alias c=clear. Generally leave the rest of that file in place, you’re just wanting to add a line anywhere you want in the file.
  4. Save out the file by hitting Ctrl + O (in Nano).
  5. Quit Nano by hitting Ctrl + X (in Nano).
  6. Close the terminal window.
  7. Open a new terminal session, usually by hitting “New window” in your terminal emulator.
  8. Try out your alias by typing c and then hitting Enter. You should see your terminal clear.

If the above steps don’t work, you may not have saved your .bashrc file, or your new session may not be loading it. The second problem is beyond the scope of this Quick Guide, the first just requires you re-do the steps we did. Happy hacking!

Yay! 🎉 You made it to the end of the article!
David Hayes
Share:

8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Scott
March 5, 2019 2:46 pm

Also, note that you don’t need to close the trminal to have the aliases take effect. You should be able to type

source .bash_profile

and the file will reload.

mountainrescuer
March 7, 2019 5:59 pm
Reply to  Scott

Thanks for the additional info Scott.

This, plus the initial info from David (and T13E) has saved me so much time!

Adriana
January 24, 2020 1:06 pm

Thanks a lot David and Scott. this hint has minimized time spent not only on commands but also navigating between directories.

T13E
February 28, 2019 2:50 am

On OSX Mojave, .bash_profile seems to be the correct place to add this. Thanks for the guide!

Anton
January 14, 2020 1:56 pm
Reply to  T13E

You can just add the following to .bash_profile and the .bashrc file will work as above:

if[ -f “$HOME/.bashrc” ] ; then
source $HOME/.bashrc
fi

Mats
January 11, 2019 9:44 am

If the file .bashrc has the following text

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

All aliases are stored in that file.

Sunil
January 16, 2019 12:29 pm
Reply to  Mats

Thank you

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!