English

Create aliases for commands on Unix

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!

bgasparotto

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

2 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

2 years ago