Previous Section  < Day Day Up >  Next Section

9.4. Aliases

An alias is a C/TC shell user-defined abbreviation for a command. (For tcsh enhancements to the alias mechanism, see "TC Shell Aliases" on page 491.) Aliases are useful when 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 .cshrc or .tcshrc file. Because the .cshrc or .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.

9.4.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.20.

% alias

co         compress

cp         cp –i

ls1        enscript –B –r –Porange –f Courier8 !* &

mailq      /usr/lib/sendmail –bp

mroe       more

mv         mv –i

rn        /usr/spool/news/bin/rn3

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.4.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.

Example 9.21.

1   % alias m more

2   % alias mroe more

3   % alias lF 'ls -alF'

4   % alias cd  'cd \!*; set prompt = "$cwd   >"'

    % cd ..

    /home/jody >  cd /         # New prompt displayed

    / >


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 definition is enclosed in quotes because of the whitespace. The alias lF is a nickname for the command ls –alF.

  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.

9.4.3 Deleting Aliases

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

Example 9.22.

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.4.4 Alias Loop

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

Example 9.23.

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 C shell catches the problem and displays an error message.

    Previous Section  < Day Day Up >  Next Section