9.17. TC Shell Aliases

 <  Day Day Up  >  

An alias is a TC shell user -defined abbreviation for a command. Aliases are useful if a command has a number of options and arguments or the syntax is difficult to remember. Aliases set at the command line are not inherited by subshells. Aliases are normally set in the .tcshrc file. Because the .tcshrc is executed when a new shell is started, any aliases set there will get reset for the new shell. Aliases may also be passed into shell scripts but will cause potential portability problems, unless they are directly set within the script.

The TC shell has some additional preset aliases, which remain undefined until you define them. They are: beepcmd , cwdcmd , periodic , and precomd . These aliases are listed and defined in "Special tcsh Aliases" on page 494.

9.17.1 Listing Aliases

The alias built-in command lists all set aliases. The alias is printed first, followed by the real command or commands it represents.

Example 9.94.
 >  alias   apache    $HOME/apache/httpd -f $HOME/apache/conf/httpd.conf   co        compress   cp        cp i   ls1       enscript B r Porange f Courier8 !* &   mailq     /usr/lib/sendmail bp   mc        setenv MC '/usr/bin/mc -P !*'; cd $MC; unsetenv MC   mroe      more   mv        mv i   uc        uncompress   uu        uudecode   vg        vgrind t s11 !:1  lpr t   weekly    (cd /home/jody/ellie/activity; ./weekly_report; echo   Done)  

EXPLANATION

The alias command lists the alias (nickname) for the command in the first column and the real command the alias represents in the second column.

9.17.2 Creating Aliases

The alias command is used to create an alias. The first argument is the name of the alias, the nickname for the command. The rest of the line consists of the command or commands that will be executed when the alias is executed. Multiple commands are separated by a semicolon, and commands containing spaces and metacharacters are surrounded by single quotes.

FORMAT

 alias alias aliasname command alias aliasname 'command command(s)' unalias aliasname 

Example 9.95.
 1   >  alias m more  2   >  alias mroe more  3   >  alias lf ls-F  4   >  alias cd 'cd \!*; set prompt = "%/ > "'  5   >  cd ..  6   /home/jody >  cd /   # new prompt displayed  / > 7   >  set tperiod = 60  >  alias periodic 'echo You have worked an hour, nonstop'  8   >  alias Usage 'echo "Error: \!* " ; exit 1'  

EXPLANATION

  1. The nickname for the more command is set to m .

  2. The alias for the more command is set to mroe . This is handy if you can't spell.

  3. The alias lf is a nickname for the tcsh built-in command ls “F . It lists files like ls “F , but is faster.

  4. When cd is executed, the alias for cd will cause cd to go to the directory named as an argument and will then reset the prompt to the current working directory ( %/ ) followed by the string "%/ > " . The !* is used by the alias in the same way it is used by the history mechanism. The backslash prevents the history mechanism from evaluating the !* first before the alias has a chance to use it. The \!* represents the arguments from the most recent command on the history list. The alias definition is enclosed in quotes because of the whitespace.

  5. After the cd command changes to the parent directory, the prompt is expanded to the current working directory ( %/ ) and a > symbol. [a]

    [a] If using /bin/csh as your shell, replace %/ with $cwd when setting the prompt.

  6. The new directory is /home/jody , which is reflected in the prompt; after changing directory to root ( / ), the prompt again reflects the change.

  7. The tperiod variable is set to 60 minutes.The alias, periodic , is a preset alias. Every 60 minutes, the echo statement will be displayed.

  8. This alias is useful in scripts to produce a diagnostic message and to then exit the script. For an example of this alias in use, see Example 10.20 on page 545.

9.17.3 Deleting Aliases

The unalias command is used to delete an alias. To temporarily turn off an alias, precede the alias name by a backslash.

Example 9.96.
 1   >  unalias mroe  2   >  \cd ..  

EXPLANATION

  1. The unalias command deletes the alias mroe from the list of defined aliases.

  2. The alias cd is temporarily turned off for this execution of the command only.

9.17.4 Alias Loop

An alias loop occurs when an alias definition references another alias that references back to the original alias.

Example 9.97.
 1   >  alias m more  2   >  alias mroe m  3   >  alias m mroe   # Causes a loop  4   >  m datafile   Alias loop.  

EXPLANATION

  1. The alias is m . The alias definition is more . Every time m is used, the more command is executed.

  2. The alias is mroe . The alias definition is m . If mroe is typed, the alias m is invoked and the more command is executed.

  3. This is the culprit. If alias m is used, it invokes alias mroe , and alias mroe references back to m , causing an alias loop. Nothing bad happens. You just get an error message.

  4. Alias m is used. It is circular. M calls mroe and mroe calls m , then m calls mroe , and so on. Rather than looping forever, the TC shell catches the problem and displays an error message.

9.17.5 Special tcsh Aliases

If set, each of the TC shell aliases executes automatically at the indicated time. They are all initially undefined.

Table 9.19. tcsh Aliases

Alias

What It Does

beepcmd

Runs when the shell wants to ring the terminal bell

cwdcmd

Runs after every change of working directory

periodic

Runs every tperiod minutes; e.g.,

 > set tperiod = 30 > alias periodic date 

precmd

Runs just before each prompt is printed; e.g., alias precmd date


 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net