Improving efficiency on Linux with bash scripts and alias commands

By Chris Taylor, published Tuesday, March 10th, 2015

Something that I use in my day-to-day development routine is alias commands on Linux. These are a shorthand way of expressing multiple or complex bash commands in your terminal to help with efficiency.

At the moment we are developing an application with AngularJS, it communicates with a RESTful API that I spend most of my time working on. I use my terminal to quickly navigate to the AngularJS repo and serve the latest stable version.

I used to run five separate commands to achieve this which looked like:

$ cd /path/to/folder/frontend/     # Navigate to the folder.
$ git pull origin development      # Pull down the latest changes.
$ npm install                      # Make sure I've got all the
$ bower install                    # dependencies installed.
$ grunt serve                      # Start the server on my local machine

This used to take about half a minute or more and I used to switch away from my terminal while it was processing and then forget about it.

I then started to chain the commands together like this:

cd /path/to/folder/frontend/ && git pull origin development && npm install && bower install && grunt serve

But even that would take me a little while to type and I didn’t like that. Using my limited knowledge of the terminal on Linux, I set up a command alias. This is basically telling the computer that when I type one thing in the terminal, I really mean another thing.

A good page to explain in more detail about setting them up is here, but I’ll show you how to set up a simple one now.

First you need to open up your .bashrc file (in your home directory) in your favourite text editor and look for some lines that look like this:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

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

If you don’t see anything like that, don’t panic! It just means that you can put your aliases directly in this file. Otherwise if you do have something like this, open up or make the file that it references (in this case .bash_aliases).

Now for my purposes, I was able to put a line in there that looked like this:

alias run-frontend='cd /path/to/folder/frontend/ && git pull origin development && npm install && bower install && grunt serve'

So just add what you need in there and save it. Then run this command:

. ~/.bashrc

Which basically tells the computer to reload the .bashrc back into memory, including our changes, and then you’ll be able to run your short command, run-frontend, and it will do all the commands for you.

Now this is pretty good and handy, but I thought this probably isn’t the best way of doing things.

For instance, what if the repo wasn’t on the ‘development’ branch, or I had un-committed changes. Both of these situations may cause the AngularJS app to not start correctly or not start at all.

What I thought would be good is a script that checks all of this stuff and then keeps me notified without needing to check my terminal.

Using this site, I quickly learnt how to write a bash script that would do exactly what my alias was doing. For reference though I have put an example script and alias below:

I saved my script under ~/scripts so I could keep multiple versions in the same place.

Here’s the content of my file:


cd /path/to/folder/frontend/ 
git pull origin development 
npm install
bower install
grunt serve

And here’s the alias needed to run it:

alias minute-surveyor='sh ~/scripts/'

When you set this up, just remember to run . ~/.bashrc to load in any changes to your current session.

In my spare time I will look at making my script do the following things:

  • Check if any commands failed and maybe use alert to show me any errors
  • Use screen to set up the application to run in the background
  • Use gnome-open to open up a browser once the application is running

But until then, this should help if you want to run lots of commands repetitively without worrying about having to type them out each time.