Skip to content

Create aliases for commands on Unix

linux shell

This tutorial will show how to create aliases for commands on Unix based systems, so you can run any set of commands with a simple shortcut.

An alias is a shortcut for a different and sometimes more complicated command, which is integrated to your bash so you can run them from any path with the help from auto-completion. for example, on Linux Mint, we have the following aliases defined by default:

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

This means that running la in your shell will be the same as running ls -A

Creating your own aliases

Either on Linux or Mac, it’s a good idea to define your own aliases in a dedicated file which will be imported in your main bash configuration. The location of the main bash configuration files varies on different systems.

Bash

I’m currently using Bash on my Linux so my main configuration file is ~/.bashrc. If I read this file, I can see it will import ~/.bash_aliases if one is present:

cat ~/.bashrc
...
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

So all we have to do is to create this file and provide some aliases inside. For example:

vi ~/.bash_aliases
java-8() {
  sdk use java 8.0.212-amzn
}

java-11() {
  sdk use java 11.0.5-amzn
}

After we save the aliases file and reload our terminal, we can simply type java-8 and the SDKMAN command I defined will be executed:

java-8
Using java version 8.0.212-amzn in this shell.

Hope it helps!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *