DEV Community

Anmolpreet Kaur
Anmolpreet Kaur

Posted on

How to Save Valuable Time with Bash Aliases

Alt Text

As a new programmer, discovering a mystical terminal in my computer was astonishing. But as I started to code, I found it very annoying when I had to type the same commands over and over again into my terminal. I was pleasantly surprised when I learned about bash aliases that I could write to save time!

You can write bash aliases to shorten simple commands as well as bash functions to put multiple commands together. On Unix-based operating systems, custom bash commands are a life-changer! Just follow these simple steps:

Open your terminal.

Enter these commands to view hidden files:

cd ~
ls -a

Open your bash profile.

You should see a list of files after typing the commands above. If your computer uses a login shell, open .bash_profile in any text editor. If your computer uses a non-login shell, open .bashrc in any text editor.
To check if your computer uses a login shell, type the following command in your terminal:

echo $0

If it returns -bash (with “-” as the first character), you are in a login shell. If it returns bash (where “-” is NOT the first character), you are in a non-login shell.
If you are unsure about how to open a file from your terminal, look here.

Once the bash profile is open, you are ready to write your custom bash commands at the bottom of the file!

To write an alias, use the following format:

alias c='clear'

This alias will allow you to just type c into your terminal every time you want to clear it.

More examples of useful aliases:

alias .='cd ..'
alias gc='git clone'
alias labs='cd ~/Flatiron/Mod2/Labs/'
alias bi='bundle install'
alias gu=''

You can write a function to put multiple commands together like this:
function gacp() {
git add . && git commit -m "$1" && git push origin $2

If you need to pass arguments into the function, the first parameter will be stored in $1, the second in $2, and so on. When using GitHub, using the function above will be very helpful. To add, commit with a message, and push to the master branch, all you have to type into your terminal is:
gacp "CLI done" master

Once you have written your aliases, save the file and open your terminal to use them!

(Note: If your terminal was already open, you may need to close it and open it again for the newly saved aliases to work.)

Alt Text


Difference between Login Shell and Non-Login Shell
Creating new bash commands and aliases
An Intro to Git Aliases: a faster way of working with Git

Top comments (0)