Bash Aliases To Save Time

Last Updated: Feb. 17th 2022 at 6:36pm Tags: bash blog linux

Creating an alias allows saves you time by reducing the amount of characters you have to type.

Back in 2009, I was inspired by this great article about Command Aliases in Unix shells. I felt so inspired by this post, I’m writing about how I use aliases to speed up web development in 2020.

Introduction

If you don’t have a basic understanding of the command line or shell, this article is not for you.
If you are familiar with basic command such as: ls, cd, cp, mv you should be able to follow along. If you already know what bash aliases are, you probably don’t need to read on, but I do share some of my favorite alias'.

Creating an alias allows saves you time by reducing the amount of characters you have to type.
This is a subject that is easier to learn by example, but here is the detailed explanation from Wikipedia.

All the below examples should work in both Borne Again Shell (bash) and zshell (zsh).

Easy Example

Starting with something simple:

The ls command is probably the most used command on the Linux command line. While I was doing some basic server administration, Bruce Alderson suggested I set-up an ll alias. I was constantly using ls -l to view files, and could save precious keystrokes by creating an alias.
This is how you create an alias for ll to run the command ls -l.

alias ll='ls -l'

Now whenever you type “ll” you will receive the same results as you would typing “ls -l”.
Here is the gotcha, it is very important to remember that an alias will only last for your current session. In order to keep the alias around permanently you have to add the alias to your .bash_aliases file in your home directory. Do some research into your current environment and see what file is best for alias' (some options would be .profile or .bashrc as opposed to .bash_aliases).

Advanced Example

In your .bash_aliases file you can also include functions, for example:

function cd_up() {
  cd $printf "%0.0s../" $(seq 1 $1));
}
alias 'up'='cd_up'

Notice the $1. You can also grab parameters the same you would in a bash script.

With this alias you can type “up 5” instead of “cd ../../../../..”.

My Aliases

As I mentioned before it’s easier to learn by example, so here I’ve included my aliases file with some examples.
I change my aliases file as often as weekly so this may be a little out of date.
I keep aliases file in sync using Ansible, so i just change my repository bash_aliases then Ansible will update it on the rest of my nodes.

I don’t know everything, so it would be great if you could leave a comment on what you find helpful, tips, tricks or areas that I could do differently.

Further Notes

  • You can remove an alias using unalias*
  • You can see available aliases by using alias with no parameters

Reference

Comments

You need to login to comment.