Today we will see how to set an alias in Linux/Unix and save time while typing. Hope this alias will save you a number of hours. This will greatly help you to concentrate on your learning rather than just entering commands. We have to spend as less time as possible in typing and use that time for some meaningful stuff. Below are some things which we will learn today.

What is an alias?

What are advantages of alias?

For which commands we have to use alias?

How to define an alias?

How to define an alias permanently?

How to define global alias permanently?
How to use an alias?

How to view set alias in Linux machine?

How to unset an alias? 

Alias command with examples

Advanced alias? Then use functions.

 

What is an alias?

An alias is a command which is another name given to a person, place, etc. For example, the country Germany has another name as Deutschland which is very much popular within Europe. In Linux/Unix too we have an alias which will help us to use a string to execute actual Linux command or group of Linux/Unix commands. In one sentence, an alias is a shortcut to execute a command or group of commands.

Suppose we want to execute whoami command, instead of typing all these six characters to run whoami command we can just type ‘p’ after setting alias. We will see how to set one below.

What are the advantages of alias?

There are many advantages of writing alias and keeping them in user property files like .bashrc and .profile file in users home directories. Some of the advantages of alias as listed below

1) Time-saving: We can save a good amount of time by shortening the length of a command so that we no need to type many characters.

2) We no need to remember bigger command (Ex: named-checkzone), we just create a short name for it and use it.

3) We can run multiple commands (using command chaining) with just single alias.

4) We can use functions to make our alias more powerful, though we do not use alias command directly.

Which commands to put alias?

As alias provide us with time-saving it is not advisable to put all the commands as an alias, this, in turn, will give you a headache to remember new alias. Then what commands we have to keep an alias?

1) Frequently executed commands. To check which commands executed frequently we can use history command and hash command for that particular session.

2) Bigger commands, which are time-consuming to type.

How to define alias?

alias a='cmd'
 alias a='cmd1;cmd2'

Examples: Setting alias for ‘ls -l’

alias ll=’ls -l’

How to define an alias permanently?

The above-mentioned method will not keep your alias across reboots and logouts we have to make these alias available all the time. To do that we have to edit our shell profiles files(~/.bashrc for BASH shell and ~/.profile for KSH shell) and give alias entries in it as shown below.

$vi ~/.bashrc

Give entries as below

alias ll=’ls -l’

Once done, save the file and exit.

Once you did this we have to source this configuration file otherwise we cannot use our new alias without system reboot.

Sourcing a file in Linux:

$source ~/.bashrc

How to use/execute an alias?

We just have to type the alias name and press enter

Example:

ll

output:

[linuxnix@ssh01 ~]$ ll
total 0
-rw-rw-r-- 1 visplad visplad 0 Mar 13 09:55 abc
[linuxnix@ssh01 ~]$

How to define global alias permanently?

We can set global alias so that these aliases are available for all the users

How to see what alias is set?

use one of the below commands to list all alias defined in your machine.

compgen -a 
or 
alias -p 
or 
alias

Output:

[linuxnix@ssh01 ~]$ alias alias l.='ls -d .* --color=auto'
alias ll='ls -l'
alias ls='ls --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

How to unset an alias?

We have to use the unalias command to unset any alias which is defined.

unalias aliasname

Example:

unalias ll

Output:

[linuxnix@ssh01 ~]$ unalias ll
[linuxnix@ssh01 ~]$ ll
-bash: ll: command not found
[linuxnix@ssh01 ~]$

Now try to execute the ll command you should not see ls -l command output and will get command not found an error.

Alias command with examples

Let us start learning Linux alias command with examples

Example1: Create an alias for getting memory states on the machine

Alias RAM=’free -m’

Output:

[linuxnix@ssh01 ~]$ alias RAM='free -m'
linuxnix@ssh01 ~]$ RAM             
total       used       free     shared    buffers     cached Mem:         15936        521      15414          0        137        157 -/+ buffers/cache:        225      15710 Swap:         8039          0       8039 [linuxnix@ssh01 ~]$

Example2: Defining multiple aliases within a single line

alias p="pwd"; l="ls -al"

Example3: Alias support command chaining as well such as semi-colon, pipe, && and || with these operations we can run multiple commands

A couple of examples on this:

Example: Write an alias which shows RAM, uptime, OS details

Example:

[linuxnix@ssh01 ~]$ alias sd='free -m;w;uname -a'
[linuxnix@ssh01 ~]$ sd             
total       used       free     shared    buffers     cached Mem:         15936        521      15414          0        137        157 -/+ buffers/cache:        225      15710 Swap:         8039          0       8039  01:06:37 up 330 days,  8:39,  1 user,  load average: 0.00, 0.00, 0.00 USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT linuxnix  pts/0    145.230.134.71   00:41    0.00s  0.26s  0.00s w Linux ssh01.plan 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux [linuxnix@ssh01 ~]$

What about command chaining with an alias?

Below are some alias commands which use command chaining

Semicolon operator

alias a=’cmd1;cmd2;cmd3’

Pipe operator

alias b=’cmd1 | cmd2 | cmd3’

Logical AND operation

alias c=’cmd1 && cmd2 && cmd3’

Logical OR operation

alias d=’cmd1 || cmd2’

In fact, you can use many of the command chaining options with an alias.

Advanced alias? Then use functions.

Sometimes the normal alias stuff which we learn will not solve our problems. One example is passing variables to the alias is not possible. If you want your alias to accept variables so that we can do more meaning full stuff then we have to use shell functions.

Example:

Alias_name () {  command1 command2 }

Example 4: Suppose you want to check free available RAM in the machine use below alias (function)

FM () {  echo "Present free RAM is `free -m | awk '{if(NR==3)print $4}'` Mb"  }

Copy this function to users profiles like ~/.bashrc for BASH shell and ~/.profile for KSH shell and source the file.

$source ~/.bashrc

Example:

$FM  Present free RAM is 15710 Mb

Example5: As said we can customize functions and send values to function alias. Suppose you want to add two numbers at the prompt, write below function into ~/.bashrc file.

SUM () { echo “Sum of two given numbers is $(($1+$2))” }

Once given entry in ~/.bashrc file source it

$source ~/.bashrc $SUM 5 6 11

Note: When we give values after function, the function will take those values into $1, $2, $3 and so on respectively.

Note: If you observe we are giving alias as capital letters, this just a convention and not mandatory. This will clearly solve below mention problem “Alias and command have the same name”.

Some FAQ’s Linux alias command:

Q) I have an alias as xyz but there is a command with this name as well, which one will execute either my alias or the command?

In that case, the alias will take precedence. Suppose if you have a command which is in built-in, alias and external commands the order of precedence is an alias, in-built commands then external commands.

Q) O.K that is fine if I want to execute the command instead of alias how can I do that?

For this, there are two solutions

1) Unset the alias with unalias command as shown above.

2) Use \ before the command as shown

\ls

To execute ls external command instead of alias stuff.

Q) Can I pass arguments to an alias?
No, if you want to pass arguments use functions as shown above examples.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.