Linux alias command explained with examples

Today we will see how to set alias in Linux/Unix and save the time while typing. Hope this alias will save you 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 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 other name given to a person, place etc. For example the country Germany has other name as Deutschland which is very much popular with in Europe. In Linux/Unix too we have 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 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 are 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 stuff it is not advisable to put all the commands as an alias, this in turn will give you headache to remember new alias. Then what commands we have to keep as 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 done 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




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


How to see what alias are set?

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

	compgen -a 
alias -p 


	[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 unalias command to unset any alias which is defined.

	unalias aliasname


	unalias ll


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


Now try to execute ll command you should not see ls -l command output and will get command not found 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’


	[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 alias within 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

Couple of examples on this:

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


	[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   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 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 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 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.


	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


	$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 in to ~/.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 in to $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 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 alias will take precedence. Suppose if you have a command which is in built in, alias and external commands the order of precedence is 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 show


To execute ls external command instead of alias stuff.

Q) Can I pass arguments to alias?

No, if you want to pass arguments use functions as shown above examples.

Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • Kuldeep Chaturvedi

    And, here are the ways to temporarily disable an alias:

    1. Use backslash () preceeding the command. If there is a command existing in $PATH, and also an alias is defined with the same name, then the command from $PATH will be used.

    For e.g., ls will run the command /usr/bin/ls (and not the alias ls, which is generally aliased to ls –color=auto on most of the Linux distributions).

    2. Use command preceeding the command. The explanation as above.


    • Surendra Anne

      Thats true. Thanks for sharing info.

  • somanath

    Nice One!!!