Create Aliases in Bash

By  on  

Every developer likes a shortcut -- they're what make us more efficient in our work.  Of course there are good shortcuts and bad shortcuts (lazy coding, lack of security review, etc.), but let's stick with the positive and talk about a good shortcut:  bash aliases.

We all have commands that we execute regularly but aren't able to remember or simply don't care to constantly type, like removing all Docker images and containers or bringing down and instantly bringing up a docker project.  Most commands we execute often each day are boilerplate; maybe one or two parameters change.  Let's have a look at how easy it is to create aliases so you can be more productive!

Creating a Basic Alias

To create an alias, start by opening ~/.bash_profile in any text editor you have available.  The format for creating an alias is as follows:

# alias-name='command to do thing'

alias docker-refresh='docker-compose down && docker-compose up'
alias serve-dir='python -m SimpleHTTPServer'

I recommend naming your aliases in a way that wont conflict with existing or future executables.  You could add a "namespace" or prefix to them, for example.  To ensure the alias will work in your shell once you're done editing .bash_profile, execute the following:

source ~/.bash_profile

Creating a basic alias is fairly simple but what if your alias requires the use of arguments?  That case is a bit different.

Aliases with Arguments

If the mostly boilerplate command we want to execute requires an argument or two, we'll need to use something more advanced that the basic bash alias format -- we'll need a function.

Let's say we want to execute a command that requires one argument -- we'd do something like this:

# Serve a directory on a given port
# https://davidwalsh.name/serve-directory-python
# $1 = port
# Example: servedir 8080
servedir() {
  # Allow myself to change the port ($1)
  python -m SimpleHTTPServer "$1"
}

# Scrape images with wget
# https://davidwalsh.name/scrape-images-wget
# $1 = url
# Example: scrapeimages https://davidwalsh.name/
scrapeimages() {
  wget -nd -H -p -A jpg,jpeg,png,gif -e robots=off $1
}

# Remove audio from video
# https://davidwalsh.name/remove-audio-video
# $1 = source file
# $2 = destination
# Example: removeaudio myvideo.webm myvideo-silent.mp4
removeaudio() {
  ffmpeg -i $1 -vcodec copy -an $2
}

Your functions can host contain any number of statements so that you can create advanced commands:

startover() {
  echo 'Killing everything'
  rm -rf build/
  docker-compose down
  docker rm -f $(docker ps -a -q)
  docker rmi -f $(docker images -q)
  echo 'Everything killed, my lord'
}

Aliases are super easy to create and can increase your productivity.  For years I was too lazy to take a few moments to create aliases and, looking back, it was a laziness that cost me.  As you continue developing a new project, be sure to take a few moments to aliases for often-used commands -- you'll thank yourself later.

Recent Features

  • By
    39 Shirts – Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Firefox Marketplace Animated Buttons

    The Firefox Marketplace is an incredibly attractive, easy to use hub that promises to make finding and promoting awesome HTML5-powered web applications easy and convenient. While I don't work directly on the Marketplace, I am privy to the codebase (and so...

  • By
    Image Data URIs with PHP

    If you troll page markup like me, you've no doubt seen the use of data URI's within image src attributes. Instead of providing a traditional address to the image, the image file data is base64-encoded and stuffed within the src attribute. Doing so saves...

Discussion

  1. Amanda Bellefeuille

    Great post! I just finally got to updating my alias’ to help me jumping in-between projects. I didn’t think about providing an argument. Just implemented it and it works like a charm! Thanks!

  2. alias reload='source ~/.bash_profile"
    alias aliasperm='alias >> ~/.bash_profile && reload"
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!