Bash function and alias

alias command is one of the most used command in Linux.We can use alias to create a new command or to modify existing one.
The use is very simple
alias <command>="<what we want to excute>"

For example, if we want to create a new command p, that will print the working directory:
alias p='pwd'
Let see it in action:

[bob@localhost ~]$ alias p='pwd'
[bob@localhost ~]$ p

As we can see we attach the string “p” to the command pwd (print working directory), and now when the user write p and execute it, then it will run pwd.
It is important to know that alias is not recursive, so if value of the alias include the alias itself, then the alias wont run itself. This is important when we modify command (like ls for example).
So let take the following example:

[bob@localhost ~]$ alias ls='ls --color=auto'
[bob@localhost ~]$ ls
[bob@localhost ~]$

So as we can see the alias “ls” is replaced to ‘ls –color=auto’, but the ls that is included in the replaced string is not continually replaced.
So if the string value of an alias include the alias, then the alias inside the string value is not replaced.
In essence alias just replace one string with anther and then execute it.
But as we have seen in the example, we can use it to create a new command (like the p command in the example), or to modify an existing command (like the modification of the ls in the above example, ls was replaced with ls –color=auto).
If we have alias of an existing command and we want to run the original command and not the alias command (for example the original ls from the example above), then we can use the backslash “\”, before the command name.
So if the ls is an alias and we want to execute the binary ls, we can:
And if continue with the example above:

[bob@localhost ~]$ \ls

So in this case the binary (and not the alias ls), is running.

if we want to cancel alias, we can use unalias command.
so for example:

bob@ubuntu14:~$ alias ll='ls -alF'
bob@ubuntu14:~$ ll 
total 20
drwxr-xr-x 2 bob  bob  4096 Aug 19 18:27 ./
drwxr-xr-x 4 root root 4096 Aug 19 18:27 ../
-rw-r--r-- 1 bob  bob   220 Aug 19 18:27 .bash_logout
-rw-r--r-- 1 bob  bob  3637 Aug 19 18:27 .bashrc
-rw-r--r-- 1 bob  bob   675 Aug 19 18:27 .profile
bob@ubuntu14:~$ unalias ll
bob@ubuntu14:~$ ll
-su: ll: command not found

So in the example, we created an alias ll, we run it.We then remove it with unlias ll.
And then when we tried to run ll, we got “command not found”, because we had removed the alias before the second execution.

function is anther feature of bash, and it allow us to group commands together.
the structure of a function is very simple:

function <functionName> (){

In order to run a function we just<functionName> .
For example we can create a function that will give us information about our Linux distribution, and kernel.
let call the function os_info.

bob@ubuntu14:~$ function os_info(){
> echo "kernel information"
> uname -a;
> echo "distribution information"
> lsb_release -a
> }
bob@ubuntu14:~$ os_info 
kernel information
Linux ubuntu14 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
distribution information
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.1 LTS
Release:	14.04
Codename:	trusty

As we can see create a new function that when run execute uname to get information about the kernel and lsb_release to get information about the distribution.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s