Sometimes a frequently used Linux command can be very long, and when that happens, we need the alias command to help simplify the work of human input.

We can create aliases in two common ways, one for temporary aliases and the other for permanent aliases.

Let’s take a look at its help documentation:

1
help alias
img

Check alias's definition and usage

We can use the alias command to view the shortcuts owned by current user:

1
alias
img

List all the available shortcuts

Create Temporary Alias

Let’s create a temporary shortcut for a text that needs to be displayed:

1
alias show_greetings='echo Hi, I am $(whoami)'
img

Create a shortcut named show_greetings

Let’s see if the temporary shortcut we just created exists:

1
alias
img

show_greetings shortcut created successfully

We can use it as directly as we use commands:

1
show_greetings
img

Use the created shortcut, it runs echo command and output the text

Remove Temporary Alias

We can use the unalias command to remove the temporarily created shortcut.

Let’s take a look at its help documentation:

1
help alias
img

Check unalias's definition and usage

Let’s remove the show_greetings shortcut:

1
unalias show_greetings
img

Remove the show_greetings shortcut

Let’s check to see if the shortcut has gone:

1
alias
img

show_greetings shortcut removed successfully

Create Permanent Alias

We can add or remove a permanent shortcut by modifying the .bashrc file in the current user’s home directory.

We use the vim text editor to open it:

1
vim ~/.bashrc

Once we open the bashrc file, we can see some predefined permanent shortcuts:

img

After openining the .bashrc file

We can add the commands we used earlier to create the temporary shortcut:

1
alias show_greetings='echo Hi, I am $(whoami)'
img

we added the show_greeting shortcut in the .bashrc file

After saving and exiting, let’s have a check on the shortcuts list.

As we can see, the permanent shortcut we just added does not appear in the shortcuts list:

1
alias
img

the show_greetings shortcut doesn't not exist

We can use the source command to re-import the bashrc file into the running system, so that we can use the permanent shortcut we just created without having to re-login.

Let’s take a look at this command’s help documentation:

1
help source
img

Check source's definition and usage

Let’s re-import .bashrc:

1
source ~/.bashrc
img

re-import the bashrc

The newly added permanent shortcut, show_greetings, should be visible and usable:

1
alias
img

the permanent show_greetings shortcut exists

References How to Create and Use Alias Command in Linux

Buy me a coffeeBuy me a coffee