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

