Print Logo

Aliases & Functions in Bash




<<  <   >  >>

You might save some time and have a little fun by putting some aliases and functions in your .bashrc file. You can put functions or aliases in .bash_profile or .bashrc in your home directory. But by using .bashrc instead of .bash_profile, they'll be read in not only when you log in, but whenever you start a new shell such as by launching an xterm, using su, etc. Your .bash_profile is read only when you log in. You might also want to look at /etc/profile and /etc/bashrc, these are the system-wide bash initialization files which are read in for every user and serve as the defaults. These should only be changed when necessary for all users, and is usually not a good place to put functions and aliases. The idea here is to personalize your environment and make it easier for you to be lazy.

Aliases

I usually put them at the very top or the very bottom of .bashrc to keep them all together. Aliases are one-liners, nicknames for commands that are too long or hard to remember to type every time.

I get tired of typing this:
[usr-3@srv-3 usr-3]$ ls -al | more

Let's call it lam instead. But first make sure there's not already a command called lam in my path:
[usr-3@srv-3 usr-3]$ which lam
It returns nothing, so we're ok. I'll edit /usr-3/.bashrc and add the line:

alias lam="ls -al | more"

We'll have to read it in, otherwise I'd have to launch a new shell to get it:

[usr-3@srv-3 usr-3]$ source ~/.bashrc
[usr-3@srv-3 usr-3]$ lam
total 1354
drwxr-xr-x  32 usr-3   users        3072 Oct 17 08:32 .
drwxr-xr-x   5 root     root         1024 Jul 22 17:26 ..
-rw-r--r--   1 usr-3   users        5422 Oct 17 08:00 .X.err
-rw-r--r--   1 usr-3   users        5742 Dec  8  1998 .Xdefaults
-rw-r--r--   1 usr-3   users          46 May  7  1996 .Xmodmap
-rw-r--r--   1 usr-3   users        1940 Jul 22 16:06 .acrorc
-rw-r--r--   1 usr-3   users           0 Sep  2 11:23 .addressbook
-rw-------   1 usr-3   users        2285 Sep  2 11:23 .addressbook.lu
-rw-------   1 usr-3   users       12288 Oct 17 08:33 .article.swp
-rw-------   1 usr-3   users        5853 Oct 17 08:13 .bash_history
-rw-r--r--   1 usr-3   users        1379 Oct 17 08:32 .bashrc
-rw-------   1 usr-3   users       12288 Oct 17 08:32 .bashrc.swp
-rw-r--r--   1 usr-3   users           0 Nov 19  1995 .dayplan
-rw-------   1 usr-3   users           0 May  8  1996 .dayplan.priv
-rw-r--r--   1 usr-3   users         208 Nov 17  1995 .dvipsrc
-rw-r--r--   1 usr-3   users        4143 Mar 15  1999 .emacs
-rw-r--r--   1 usr-3   users        1168 Feb  3  1998 .exrc
drwxr-xr-x  13 usr-3   users        1024 Sep 30 16:18 .gimp-1.1
-rw-r--r--   1 usr-3   users        5376 Aug 28  1996 .gimprc
drwx------   2 usr-3   users        1024 Jan  2  2000 .grok
--More--
Some other examples:

Get a summary of the disk usage in human readable format
alias duh="du -h --max-depth=1"

[usr-3@srv-3 music]$ duh .
689M    ./d7
192M    ./misc_tunes
749M    ./mike
1.6G    .

kill everything netscape:
alias dienet='kill `ps auxwww | grep netscape | cut -c10-15`'
(notice the use of single quotes around the alias, double quotes take away the command substitution magic of the backticks.)

kill it really dead:
alias dienet9='kill -9 `ps auxwww | grep netscape | cut -c10-15`'

You can also define aliases on the command line, but of course they won't persist beyond the life of the shell.

[usr-3@srv-3 music]$ alias smithandjones="ssh -l smith jones"
[usr-3@srv-3 music]$ smithandjones
smith@jones's password: 
Linux 2.2.16.
No mail.
jones:/home/smith$ 

Back on srv-3, if I launch a new shell I lose the alias:

[usr-3@srv-3 music]$ bash
[usr-3@srv-3 music]$ smithandjones
bash: smithandjones: command not found

Functions

A function is like a shell script that you can put in your .bashrc file. This is good for things that won't fit on a single command line or which require logic or looping. As with aliases or shell scripts, work out the commands at the prompt before you try to run them as a function from your files. Unlike a shell script, no new process is executed to interpret functions.

Here's a function for something I get tired of doing by hand:

function fame {
wget http://www.slashdot.org -O ~/slashdot.html
FAME=`grep "Urbana Dergahad" ~/slashdot.html`
if test -n "$FAME"
then
mail mom -s "Look Ma I'm Famous" < ~/slashdot.html
else echo "sorry...."
fi
}

So I add it to my .bashrc and read it in:

[usr-3@srv-3 usr-3]$ vi .bashrc
[usr-3@srv-3 usr-3]$ . .bashrc
[usr-3@srv-3 usr-3]$ fame
sorry....

Always the same! Oh well. You might be able to think of some more practical and less hopeless examples!




This article comes from NetAdminTools:
http://www.netadmintools.com/

The URL for this story is:
http://www.netadmintools.com/art2.html

Copyright 1997-2009 NetAdminTools.com. Read our Terms of Use.