Examples of creating command alias in the Bourne Again SHell

To create a simple alias:

   alias del='rm -i'
   del memo.txt
   rm: remove memeo.txt? y

This creates the alias del for the command rm -i which prompts you for confirmation that you want to remove a file before it does so.

Make this alias a permanent feature of your environment by adding it to your shell start up file .bashrc.

To create a command alias that consists of several commands:

   alias what='ps -aux | grep $USER | less'

This creates the alias what.

Entering the command what will now run the command

   ps -aux | grep $USER | less

which consists of three commands linked together with pipes.

To refer to another command alias within an alias:

   alias h=history
   alias rev='h | tail -10'

The first command assigns the alias h to the history command. The next command assigns the alias rev to the command h | tail -10. This takes the output from the alias h ( the history command) and pipes it through the tail command to list the ten most recent commands in the command history.

To use more than one alias on the same command line:

   alias root='cd /; '
   alias slist='ls -l | head -5'
   root slist
   total 11217
   drwx------    2 root     sys          512 Apr  6  1992 News
   drwxr-xr-x    2 bin      bin         2048 Feb  1 17:19 bin
   -r--r--r--    1 root     sys       146667 Nov  1 10:59 boot
   -rwxr-xr-x    1 root     sys        73483 May 16  1991 boot.bak

Provided the last character in the alias definition is a blank space ` ' then any argument to this alias as also checked to see if it is an alias. If so, it is executed.

Notice that in the first alias definition the command ends in a ; (semicolon) to allow another command to follow it.

The command line root slist then has the following action: the root directory becomes the current working directory and its contents listed in a long format with only the first five lines of output being displayed.

[Home] [Search] [Index]