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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • 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

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!