Cari di Shell Script 
    Shell Script Linux Reference Manual
Daftar Isi
(Sebelumnya) 24. Function Taking Parameters ...26. List Constructs (Berikutnya)

Chapter 25. Aliases

A Bash alias is essentially nothingmore than a keyboard shortcut, an abbreviation, a means ofavoiding typing a long command sequence. If, for example,we include alias lm="ls -l | more" inthe ~/.bashrc file, then each lm [1]typed at the command-line will automatically be replaced by als -l | more. This can save a great deal oftyping at the command-line and avoid having to remember complexcombinations of commands and options. Setting aliasrm="rm -i" (interactive mode delete) may save agood deal of grief, since it can prevent inadvertently deletingimportant files.

In a script, aliases have very limited usefulness. It would benice if aliases could assume some of the functionality ofthe C preprocessor, such as macro expansion,but unfortunately Bash does not expand arguments within thealias body. [2]Moreover, a script fails to expand an alias itselfwithin "compound constructs," such as if/then statements, loops, andfunctions. An added limitation is that an alias will not expandrecursively. Almost invariably, whatever we would like an aliasto do could be accomplished much more effectively with a function.

Example 25-1. Aliases within a script

#!/bin/bash# alias.shshopt -s expand_aliases# Must set this option, else script will not expand aliases.# First, some fun.alias Jesse_James='echo ""Alias Jesse James" was a 1959 comedy starring Bob Hope."'Jesse_Jamesecho; echo; echo;alias ll="ls -l" # May use either single (') or double (") quotes to define an alias.echo "Trying aliased "ll":" ll /usr/X11R6/bin/mk*   #* Alias works.echodirectory=/usr/X11R6/bin/prefix=mk*  # See if wild card causes problems.echo "Variables "directory" + "prefix" = $directory$prefix" echoalias lll="ls -l $directory$prefix" echo "Trying aliased "lll":" lll # Long listing of all files in /usr/X11R6/bin stating with mk.# An alias can handle concatenated variables -- including wild card -- o.k.TRUE=1echoif [ TRUE ]then  alias rr="ls -l"   echo "Trying aliased "rr" within if/then statement:"   rr /usr/X11R6/bin/mk*   #* Error message results!  # Aliases not expanded within compound statements.  echo "However, previously expanded alias still recognized:"   ll /usr/X11R6/bin/mk*fi  echocount=0while [ $count -lt 3 ]do  alias rrr="ls -l"   echo "Trying aliased "rrr" within "while" loop:"   rrr /usr/X11R6/bin/mk*   #* Alias will not expand here either.   #  alias.sh: line 57: rrr: command not found  let count+=1done echo; echoalias xyz='cat $0'   # Script lists itself. # Note strong quotes.xyz#  This seems to work,#+ although the Bash documentation suggests that it shouldn't.##  However, as Steve Jacobson points out,#+ the "$0" parameter expands immediately upon declaration of the alias.exit 0

The unalias command removes a previously set alias.

Example 25-2. unalias: Setting and unsetting an alias

#!/bin/bash# unalias.shshopt -s expand_aliases  # Enables alias expansion.alias llm='ls -al | more'llmechounalias llm  # Unset alias.llm# Error message results, since 'llm' no longer recognized.exit 0
bash$ ./unalias.shtotal 6drwxrwxr-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

Notes

[1]

... as the first word of a command string. Obviously, an alias is only meaningful at the beginning of a command.

[2]

However, aliases do seem to expand positional parameters.


Copyright © 2000, by Mendel Cooper <[email protected]>
(Sebelumnya) 24. Function Taking Parameters ...26. List Constructs (Berikutnya)