Important notice about this site

I graduated from the University of Texas at Austin in December 2020. This blog is archived and no updates will be made to it.

Shell aliases and how you can use them

July 31, 2019

Aliases are a wonderful feature of most shells (fancy name for terminal environments) that you can make use of to make using the terminal a much easier experience.

The most common shell in use today is Bash shell. However, another popular shell with features added to Bash is Zsh (z shell, also pronounced "zosh"). Whether you use Bash or Zsh, setting an alias works the same way, except you'll want to put aliases in ~/.bashrc or ~/.zshrc respectively.

For example, here's how I define aliases in my ~/.zshrc file:

alias kc="kubectl"
alias g="git"
alias gpl="git pull"
alias gac="git add . && git commit -m"
alias gaa="git add ."
alias gcm="git commit -m"
alias gps="git push"
alias gco="git checkout"
alias d="docker"
alias dbd="docker build ."
alias drd="docker run -e ABC=def -p 1234:1234 -it "
alias dcl="docker container ls"

You'd use them as if they were a real shell command. (Well, they technically are, but you made them!) For instance, if you used to do this:

$ git add .
$ git commit -m "Some commit message"

you can do this instead:

$ gac "Some commit message"

and this saves so much time!

These aliases come in handy whenever I use some common commands that are long and tedious to type out each time.

If you want to make a function, or a "compound alias" as I like to think of it, you can define it in a familiar notation as C functions. For instance, these two functions do multiple things at once:

copyb64e() {
  pbpaste | base64 | pbcopy
}
copyb64d() {
  pbpaste | base64 --decode | pbcopy
}

You can also take in positional arguments into your new functions and refer to the arguments by the order in which they are passed in.

printb64e() {
  echo $1 | base64
  echo "$1 is now $($1 | base64)"
}
printb64d() {
  echo $1 | base64 --decode
  echo "$1 is now $($1 | base64 --decode)"
}

Final tip: once you modify your ~/.bashrc or ~/.zshrc file, you'll want to refresh your shell by doing source ~/.bashrc or source ~/.zshrc respectively.

In summary, shell aliases are really convenient for making your terminal experience more enjoyable.

Back to blog

Back to top