In our previous post, we went over the uses for bash aliases and how you can figure out which will be most effective at increasing your productivity. Today we want to go over how you set them up and some organizational patterns we use.
Previous Post: Do You Need Bash Aliases
You are probably here because you want to be more efficient on the command line but are not sure where to begin. We need to start by having a dedicated place to put aliases, and that place is ~/.bash_aliases
. Let's figure out how to do that.
What is an alias
But first, we should describe what an alias even is. An alias is simply a shortcut to a command. Aliases are specific to the particular session you have going unless you follow the steps outlined in this post. Below are a couple of commands to manage aliases.
Command | Description |
---|---|
alias |
List all aliases |
alias la="ls -lA" |
Create an alias of la that runs ls -lA |
unalias la |
Remove the alias of la |
Step 1: Find your entry point
Depending on your Operating System, you might have a different entry point. We are going to temporarily add an echo to the top of the following files to determine your entry point: .bashrc
, .bash_profile
, and .profile
.
Edit the file
nano ~/.bashrc
Add the following line & save
echo '.bashrc'
Repeat these steps for each of the potential entry points.
Now we are going to open a new Terminal window and see what text hits the screen. We are running Mac OSX for this tutorial and we see .bash_profile
on our screen.
Last login: Thu Aug 8 12:54:07 on ttys004
.bash_profile
BatMac:~ batman$
Step 2: Clean up after Step 1
Now that we know what files get sourced, we can undo what we just did. Remove the echo statements from the files you just modified.
Edit the file
nano ~/.bashrc
Step 3: Create the aliases file
At this point, we could just add aliases directly to our entry point, but instead, to keep things clean and tidy, we are going to create a separate file to hold them. We plan to have a lot of aliases, so avoiding clutter in .bash_profile
will be helpful.
touch ~/.bash_aliases
Step 4: Source the aliases file
Next, we need to source (or load) our ~/.bash_aliases
file that we created in the previous step. Add the following code at the bottom of the entry point file you determined above. For us, it is .bash_profile
.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The if
statement ensures that the file exists before attempting to source it to prevent potential errors.
Step 5: Add aliases
Now that we have an aliases file setup and loading, let's add some convenience aliases to help make managing aliases easier. Below are two aliases that will let you edit your aliases very quickly and source them once you are done.
# Alias Management
alias nba="nano ~/.bash_aliases"
alias sba=". ~/.bash_aliases"
Step 6: Test
Once you have updated your .bash_aliases
file, open a new Terminal window, and run nba
to test that your aliases loaded. If they did not, run through the steps again and verify that you named all your files consistently.
Step 7: Add more aliases #optionalNotOptional
We know what your thinking, we showed you how to set up bash aliases but didn't give us any useful aliases. Check out our followup post, Useful Bash Aliases, where we have laid out a list of aliases we use daily.
Community
Did you have any troubles, was this beneficial, do you like waffles? Let us know here on Twitter.