3.20. Aliases

A bash alias is essentially nothing more than a keyboard shortcut, an abbreviation, a means of avoiding typing a long command sequence. If, for example, we include alias lm="ls -l | more" in the ~/.bashrc file (see Section 3.23), then each lm typed at the command line will automatically be replaced by a ls -l | more. This can save a great deal of typing at the command line and avoid having to remember complex combinations of commands and options. Setting alias rm="rm -i" (interactive mode delete) may save a good deal of grief, since it can prevent inadvertently losing important files.

In a script, aliases have very limited usefulness. It would be quite nice if aliases could assume some of the functionality of the C preprocessor, such as macro expansion, but unfortunately Bash does not expand arguments within the alias body. Moreover, a script fails to expand an alias itself within "compound constructs", such as if/then statements, loops, and functions. Almost invariably, whatever we would like an alias to do could be accomplished much more effectively with a function.


Example 3-85. Aliases within a script

   1 #!/bin/bash2
   2 
   3 shopt -s expand_aliases
   4 # Must set this option, else script will not expand aliases.
   5 
   6 
   7 # First, some fun.
   8 alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
   9 Jesse_James
  10 
  11 
  12 echo; echo; echo;
  13 
  14 alias ll="ls -l"
  15 # May use either single (') or double (") quotes to define an alias.
  16 
  17 echo "Trying aliased \"ll\":"
  18 ll /usr/X11R6/bin/mk*   # Alias works.
  19 
  20 echo
  21 
  22 directory=/usr/X11R6/bin/
  23 prefix=mk*  # See if wild-card causes problems.
  24 echo "Variables \"directory\" + \"prefix\" = $directory$prefix"
  25 echo
  26 
  27 alias lll="ls -l $directory$prefix"
  28 
  29 echo "Trying aliased \"lll\":"
  30 lll  # Long listing of all files in /usr/X11R6/bin stating with mk.
  31 # Alias handles concatenated variables, including wild-card o.k.
  32 
  33 
  34 
  35 
  36 TRUE=1
  37 
  38 echo
  39 
  40 if [ TRUE ]
  41 then
  42   alias rr="ls -l"
  43   echo "Trying aliased \"rr\" within if/then statement:"
  44   rr /usr/X11R6/bin/mk*   # Error message results!
  45   # Aliases not expanded within compound statements.
  46   echo "However, previously expanded alias still recognized:"
  47   ll /usr/X11R6/bin/mk*
  48 fi  
  49 
  50 echo
  51 
  52 count=0
  53 while [ $count -lt 3 ]
  54 do
  55   alias rrr="ls -l"
  56   echo "Trying aliased \"rrr\" within \"while\" loop:"
  57   rrr /usr/X11R6/bin/mk*   # Alias will not expand here either.
  58   let count+=1
  59 done 
  60 
  61 
  62 exit 0

Note

The unalias command removes a previously set alias.


Example 3-86. unalias: Setting and unsetting an alias

   1 #!/bin/bash
   2 
   3 shopt -s expand_aliases  # Enables alias expansion.
   4 
   5 alias llm='ls -al | more'
   6 llm
   7 
   8 echo
   9 
  10 unalias llm    # Unset alias.
  11 llm
  12 # Error message results, since 'llm' no longer recognized.
  13 
  14 exit 0

 bash$ ./unalias.sh
 total 6
drwxrwxr-x    2 bozo     bozo         3072 Feb  6 14:04 .
drwxr-xr-x   40 bozo     bozo         2048 Feb  6 14:04 ..
-rwxr-xr-x    1 bozo     bozo          199 Feb  6 14:04 unalias.sh

./unalias.sh: llm: command not found