1. Overview

It’s pretty common for Linux users to use the same commands repeatedly over time. Copy-pasting commands through terminal history is a tedious and time-consuming task that subsequently reduces user productivity. It’s always a good idea for Linux users to optimize their workflow and maximize work efficiency.

For instance, we can quickly create shortcuts for complex commands and reuse them umpteen times instead of typing or remembering them.

Aliases help us to solve this problem easily, and they’re akin to Windows shortcuts.

Now, let’s get into the nitty-gritty of it.

2. Create Aliases

Generally, an alias is a custom command defined by the user to execute a set of complex commands:

alias shortcut-name = commands-to-execute

$ du -hs /var/log/* | sort -hr | head -3 
4.1G    /var/log/journal 
239M    /var/log/btmp.1 
48M     /var/log/logstash 
$ alias top3logs='du -hs /var/log/* | sort -hr | head -10' 
$ top3logs 
4.1G    /var/log/journal 
239M    /var/log/btmp.1 
48M     /var/log/logstash 

Please note that we need to escape single and double quotes using the backslash sign if our command contains them.

For instance, in order to create an alias for the command line: echo “Path is $PATH”, we need to escate the double quotes:

$ alias currentpath="echo \"Path is $PATH\""

Alternatively, we can rewrite the above command using single quotes:

$ alias currentpath='echo "Path is $PATH"'

That way, we don’t have to escape the double quotes since they are not inside double quotes.

Now that we know how to create an alias for a command line, let’s see how many types of alias there are.

2.1. Temporary Alias

If an alias is active only as long as our session is active, we categorize it as temporary. These aliases have no effect when we open a new session or terminate the current session.

Let’s see a quick illustration:

$ date 
Tue Aug  3 05:24:14 IST 2021 

$ who -u 
tools    pts/0        2021-08-03 05:24   .         20728 (122.164.227.87) 

$ pwd 
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4 
$ alias goto_elk_jar_path='cd /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4/' 
$ pwd 
/home/tools/baeldung 

$ goto_elk_jar_path 

tools@sandbox1:~$ pwd 
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-input-dead_letter_queue-1.1.5/vendor/jar-dependencies/co/elastic/logstash/input/logstash-input-dead_letter_queue/1.1.4 

The who -u command helps us to get the current session details.

Let’s terminate the current session (PID: 20728) and open a new session (PID: 25267), as shown below.

$ date 
Tue Aug  3 05:37:04 IST 2021 

$ who -u 
tools    pts/0        2021-08-03 05:36   .         25267 (122.164.227.87) 
$ goto_elk_jar_path 
goto_elk_jar_path: command not found 

The “command not found” error tells us that we don’t have any custom alias command with the name of “goto_elk_jar_path”.

2.2. Permanent Alias

Usually, CLI is a very common touchpoint for programmers, and we need to keep the alias permanently for quick execution of commands. There are two prominent ways to add an alias permanently in Linux.

One option is to add the alias directly into the .bashrc file. Then, using the source command, we can execute the file and implement the alias in the current session itself:

vi ~/.bashrc 
alias apt-update='sudo apt update && apt upgrade -y' ## Add this line in the file and save the file using :wq!.
source ~/.bashrc 

Alternatively, we can also manage all aliases into a single file known as .bash_aliases:

vi ~/.bash_aliases 
alias apt-update='sudo apt update && upgrade -y' ## Add this line in the file and save the file using :wq!
source ~/.bashrc 

Be sure to call the .bash_aliases from the .bashrc file as illustrated below:

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

Now, let’s try to execute the alias created for apt-get repository updates and upgrades:

$ apt-update 
Hit:1 https://artifacts.elastic.co/packages/7.x/apt stable InRelease 
.. 
.. 
.. 
Reading package lists... Done 
Building dependency tree 
Reading state information... Done 
All packages are up to date. 
Reading package lists... Done 
Building dependency tree 
Reading state information... Done 
Calculating upgrade... Done 
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 

3. List the Aliases

Here, the alias command will list all active aliases that are currently in the userspace, including both temporary and permanent aliases:

$ alias | grep -E "temp|apt" 
alias apt-update='sudo apt update -y && sudo apt upgrade -y' 
alias temp_disk='du -h .' 

Likewise, the && command will ensure that only a successful execution of the first command can invoke the execution of the second command.

4. Remove an Alias

Similarly, the unalias command helps to remove both temporary and permanent aliases attached to the current user session:

$ alias temp_alias_disk='du -h .' 

$ alias | grep -E "apt|temp" 
alias apt-update='sudo apt update -y && sudo apt upgrade -y' 
alias temp_alias_disk='du -h .' 
$ unalias apt-update temp_alias_disk 

$ alias | grep -E "apt|temp" 

However, when we open a new session, any permanent aliases we removed in the previous session are still active because they’re hardcoded in the .bashrc file.

We have to remove the corresponding alias lines from the .bashrc file to remove them permanently.

5. Aliases with Arguments

Many times, an alias becomes handier when we can pass arguments to it. A simple bash function helps us to perform this operation.

General Syntax:
function fn_name {
[commands]
}

Let’s define the function in the ~/.bashrc file that creates the nested directory and then takes us into it.

Usually, after adding the code in the ~/.bashrc file, we use the source command to execute the file:

function make-dir-cd() 
{ 
    mkdir -p -- "$1/$2/$3" && cd -P -- "$1/$2/$3" 
} 

Here, $1, $2, and $3 are the arguments that are passed on CLI in the corresponding positions after the function name:

$ vi ~/.bashrc 
$ source ~/.bashrc 
$ pwd 
/home/tools 
$ make-dir-cd bael dung alias 

$ pwd 
/home/tools/bael/dung/alias 

6. Conclusion

In summary, an alias is similar to a shortcut in Windows. Using the alias command, we can create custom commands to call other commands (complex in nature or scripts). Using aliases improves productivity and can make our day-to-day workflows easier.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.