If you’re like me, you don’t like to waste time on things you don’t have to.
When it comes to the command line, I like to type as little as possible in order to reach my destination. Luckily early on, I had learned you can create shortcuts, or aliases for commands you use a lot.
Bash aliases are shortcuts for commands you use often. For example, instead of
using ls all the time, I created an alias for it:
alias ll="ls" so now I just
have to type
Instead of typing out cd .. in order to go up a directory, I have an alias
that will do the same thing.
alias ..="cd .."
Every new project I get, one of the first things I do is create an alias for where that project lives. This is especially helpful, as my parent directory is Personal Projects which would normally have me using Personal\ Projects in order to reach it each time. I know you can hit tab but I like having the alias option for quicker access.
I even have an alias for my .bashrc so I can quickly edit it and add another alias. alias bashProfile=“code ~/.bashrc” The code in the alias will open my .bashrc in VS Code. Likewise you can do the same with Sublime Text and Atom.
Well thats fun, but how do I create an alias?
Creating aliases is super easy. There are two ways to create them. The first is just typing your alias into the terminal, this will allow you to use that alias for that session, however it will disappear once that window is closed. If you want your aliases to persist all you have to do is open your .bashrc file in your favorite editor.
Once you have it open, you can start making some aliases. Aliases are created in the form of alias ALIASNAME=“COMMANDTORUN” Be sure there are no spaces between the ALIASNAME, the = sign and the “COMMANDTORUN”, spaces will break the alias.
Here are just a few of the bash aliases I have set up:
alias ..="cd .."alias ll="ls"alias ss9="python -m SimpleHTTPServer 9000"alias globalIgnore="cd ~/.gitignore_global"alias pp="cd ~/Documents/Personal-Projects/"
Once you save that file, you will either have to open a new terminal window in order for you new aliases to take effect or you can type source ~ ./bashrc in order to get access to your new aliases right away.
What’s the difference between .bashrc and .bash_profile?
The .bash_profile is executed during login shells and .bashrc is executed for interactive non-login shells. Basically if you logged in via a username and password, the bash_profile will be used, and if not the .bashrc file will be used.
If you are on a Mac, the Terminal app, and other third party apps, tend to use bash_profile by default.
So does that mean I need to manage two files? Nope.
Just put the following code into your .bash_profile and it will source the aliases and functions from your .bashrc file.
if [ -f ~/.bashrc ]; thensource ~/.bashrcfi
This is just a very brief introduction to bash aliases. You can find some more examples of bash aliases and even bash functions you can use, on the interwebs.
Now that you know what they are, start creating your own and make the command line more fun, easier to navigate, and save some time.