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
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

Incredible Demos

  • By
    Element Position Swapping Using MooTools 1.2

    We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method. MooTools 1.2 Implementation MooTools 1.2 Usage To call the swap...

  • By
    Google Extension Effect with CSS or jQuery or MooTools JavaScript

    Both of the two great browser vendors, Google and Mozilla, have Extensions pages that utilize simple but classy animation effects to enhance the page. One of the extensions used by Google is a basic margin-top animation to switch between two panes: a graphic pane...

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!