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
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

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!