Defining Aliases and Functions

Howto create and use an alias or function

Alias Command


Aliases are an extremely powerful function that is available to all users within your systems shell. An alias command allows you to launch a command or a series of commands by typing an alias name or an abbreviation to a command that you issue on a regular basis. This ability can allow you to create commands of your own. The alias functionality is built into most shells (bash, csh and ksh). One note to remember is that an alias is only valid in the shell that it is created in. The following are examples of the alias command in my bash shell:


john@john-desktop:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

The basic syntax for creating an alias command is: alias alias_name='some command(s)'





To display any alias commands that have been defined in your shell, simply type alias.

As an example, the alias "ll" has been created. Whenever I issue the command "ll" the command "ls -alF" is issued instead. Quite often an administrator will create an alias for the "rm" command:
alias rm='rm -i'. This alias adds the "-i" parameter to the "rm" command for safety. (-i asks for confirmation before deleting a file).

If you need to run multiple commands one after another, you will need to add the commands between single quotes and separate each command with a semi colon. In the following example I have created an alias command called "bigf". When issued this will display the 10 largest files in my current directory.


alias bigf='find . -type f -exec ls -s {} \; | sort -n -r | head -10'

In this example we use the "bigf" alias to find files in the current directory, then list this by size. The output is then piped into sort to sort numerically and in reverse order. This output is then passed to the head command to display the top ten lines.


john@john-desktop:~/Documents/Weather$ alias
alias bigf='find . -type f -exec ls -s {} \; | sort -n -r | head -10'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

Now when we type "bigf" at the command line, we receive the following information back:


john@john-desktop:~/Documents/Weather$ bigf
9304 ./Riddle_et_al_accepted_withFigs.pdf
7624 ./11_Scaife_Stratosphere.odp
3976 ./long_range_forecast_winter_2012-13_full_report.pdf
3388 ./weather_lecture2.pdf
2012 ./Weather_&_Climate.pdf
1944 ./weatherwise_ws1080pc-solar_manual_a.doc
1912 ./FMH1.pdf
1680 ./weather_lecture3.pdf
1444 ./weather_lecture1.pdf
1084 ./Understanding the GFS charts-2nd version-13 sept 10.pdf


Making an Alias Permanent


Although creating an alias by typing the alias command at the command line is a simple way to create a shortcut to a command. Unfortunately, it is not a permanent feature. Alias entries created in this manner only last for your current login session. This may be acceptable on larger systems where the system is not rebooted on a daily basis, however, on smaller systems this may become tedious if you have many aliases. To get around this issue, we can make an alias entry permanent by adding it into your users ".bashrc" file. This file is normally located in your home area. An example extract from my ".bashrc file" :


# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'


System Wide Aliases


System wide alias entries can be added into "/etc/bashrc" under Red Hat based systems. Under Ubuntu this file is called "/etc/bash.bashrc".

Removing Alias Entries - unalias command


So far we have seen how to quickly create an alias, now we need to know how to remove an entry. To remove an entry we use the unalias command:


john@john-desktop:~$ alias
alias bigf='find . -type f -exec ls -s {} \; | sort -n -r | head -10'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

john@john-desktop:~$ unalias bigf

john@john-desktop:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

Now we can see that our "bigf" alias has been removed.

Functions in the Bash Shell


Another popular way to create a shortcut or a mini routine is to run is a "function". Functions are similar to the way the alias command is used. In its simplest form a function can be created at the command line:


john@john-desktop:~$ function myfunction() {
> ls -lF
> }
john@john-desktop:~$

In this simple example of a function, we create a function called "myfunction()". We then open our command section with a "{", the shell will automatically expect more information to be entered. This is why we see the prompt change to a continuation prompt ">". After typing our command or commands that we wish to run we end out continuation with a "}". Now when we enter the function name "myfunction" the command "ls -lF" will be executed. One of the main uses of the "function" command is to create functions within a script. These can then be called throughout the script without the need for code replication.
Below is an exert from the output from our function "myfunction":


john@john-desktop:~$ myfunction
total 70828
drwxrwxr-x   2 john john     4096 Oct 22  2011 Audiobooks/
drwxrwxr-x   2 john john     4096 Jan 29 22:16 CentOS_iso_6.3/
drwxr-xr-x   2 john john     4096 Oct 22  2011 Desktop/
drwxr-xr-x  10 john john     4096 Feb 16 22:49 Documents/
drwxr-xr-x   3 john john     4096 Feb 17 21:56 Downloads/
-rw-r--r--   1 john john      179 Oct 22  2011 examples.desktop
drwxrwxr-x   4 john john     4096 Dec 14 20:36 Films/