|< Day Day Up >|
An alias is a (usually short) name that the shell translates into another (usually longer) name or (complex) command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. They are typically placed in the ~/.bashrc (bash) or ~/.tcshrc (tcsh) startup files so that they are available to interactive subshells.
Under bash the syntax of the alias builtin is
Under tcsh the syntax is
alias [name[ value]]
In the bash syntax there are no SPACEs around the equal sign. If value contains SPACEs or TABs, you must enclose value between quotation marks. Unlike aliases under tcsh, a bash alias does not accept an argument from the command line in value. Use a bash function (page 315) when you need to use an argument.
An alias does not replace itself, which avoids the possibility of infinite recursion in handling an alias such as the following:
$ alias ls='ls -F'
You can nest aliases. Aliases are disabled for noninteractive shells (that is, shell scripts). To see a list of the current aliases, give the command alias. To view the alias for a particular name, use alias followed by the name and nothing else. You can use the unalias builtin to remove an alias.
When you give an alias builtin without any arguments, the shell displays a list of all defined aliases:
$ alias alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' alias zap='rm -i'
Most Linux distributions define at least some aliases. Give an alias command to see which aliases are in effect. You can delete the aliases you do not want from the appropriate startup file.
Single Versus Double Quotation Marks in Aliases
The choice of single or double quotation marks is significant in the alias syntax when the alias includes variables. If you enclose value within double quotation marks, any variables that appear in value are expanded when the alias is created. If you enclose value within single quotation marks, variables are not expanded until the alias is used. The following example illustrates the difference.
The PWD keyword variable holds the pathname of the working directory. Alex creates two aliases while he is working in his home directory. Because he uses double quotation marks when he creates the dirA alias, the shell substitutes the value of the working directory when he creates this alias. The alias dirA command displays the dirA alias and shows that the substitution has already taken place:
$ echo $PWD /home/alex $ alias dirA="echo Working directory is $PWD" $ alias dirA alias dirA='echo Working directory is /home/alex'
When Alex creates the dirB alias, he uses single quotation marks, which prevent the shell from expanding the $PWD variable. The alias dirB command shows that the dirB alias still holds the unexpanded $PWD variable:
$ alias dirB='echo Working directory is $PWD' $ alias dirB alias dirB='echo Working directory is $PWD'
After creating the dirA and dirB aliases, Alex uses cd to make cars his working directory and gives each of the aliases as commands. The alias that he created with double quotation marks displays the name of the directory that he created the alias in as the working directory (which is wrong) and the dirB alias displays the proper name of the working directory:
$ cd cars $ dirA Working directory is /home/alex $ dirB Working directory is /home/alex/cars
tip: How to prevent the shell from invoking an alias
The shell checks only simple, unquoted commands to see if they are aliases. Commands given as relative or absolute pathnames and quoted commands are not checked. When you want to give a command that has an alias but do not want to use the alias, precede the command with a backslash, specify the command's absolute pathname, or give the command as . /command.
Examples of Aliases
The following alias allows you to type r to repeat the previous command or r abc to repeat the last command line that began with abc:
$ alias r='fc -s'
If you use the command ls ltr frequently, you can create an alias that substitutes ls ltr when you give the command l:
$ alias l='ls -ltr' $ l total 41 -rw-r--r-- 1 alex group 30015 Mar 1 2004 flute.ps -rw-r----- 1 alex group 3089 Feb 11 2005 XTerm.ad -rw-r--r-- 1 alex group 641 Apr 1 2005 fixtax.icn -rw-r--r-- 1 alex group 484 Apr 9 2005 maptax.icn drwxrwxr-x 2 alex group 1024 Aug 9 17:41 Tiger drwxrwxr-x 2 alex group 1024 Sep 10 11:32 testdir -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor drwxrwxr-x 2 alex group 1024 Oct 27 20:19 Test_Emacs
Another common use of aliases is to protect yourself from mistakes. The following example substitutes the interactive version of the rm utility when you give the command zap:
$ alias zap='rm -i' $ zap f* rm: remove 'fixtax.icn'? n rm: remove 'flute.ps'? n rm: remove 'floor'? n
The i option causes rm to ask you to verify each file that would be deleted, to help you avoid accidentally deleting the wrong file. You can also alias rm with the rm i command: alias rm='rm i'.
The aliases in the next example cause the shell to substitute ls l each time you give an ll command and ls F when you use ls:
$ alias ls='ls -F' $ alias ll='ls -l' $ ll total 41 drwxrwxr-x 2 alex group 1024 Oct 27 20:19 Test_Emacs/ drwxrwxr-x 2 alex group 1024 Aug 9 17:41 Tiger/ -rw-r----- 1 alex group 3089 Feb 11 2005 XTerm.ad -rw-r--r-- 1 alex group 641 Apr 1 2005 fixtax.icn -rw-r--r-- 1 alex group 30015 Mar 1 2004 flute.ps -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor* -rw-r--r-- 1 alex group 484 Apr 9 2005 maptax.icn drwxrwxr-x 2 alex group 1024 Sep 10 11:32 testdir/
The F option causes ls to print a slash (/) at the end of directory names and an asterisk (*) at the end of the names of executable files. In this example, the string that replaces the alias ll (ls l) itself contains an alias (ls). When it replaces an alias with its value, the shell looks at the first word of the replacement string to see whether it is an alias. In the preceding example, the replacement string contains the alias ls, so a second substitution occurs to produce the final command ls F l. (To avoid a recursive plunge, the ls in the replacement text, although an alias, is not expanded a second time.)
When given a list of aliases without the =value or value field, the alias builtin responds by displaying the value of each defined alias. The alias builtin reports an error if an alias has not been defined:
$ alias ll l ls zap wx alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' alias zap='rm -i' bash: alias: wx: not found
You can avoid alias substitution by preceding the aliased command with a backslash (\):
$ \ls Test_Emacs XTerm.ad flute.ps maptax.icn Tiger fixtax.icn floor testdir
Because the replacement of an alias name with the alias value does not change the rest of the command line, any arguments are still received by the command that gets executed:
$ ll f* -rw-r--r-- 1 alex group 641 Apr 1 2005 fixtax.icn -rw-r--r-- 1 alex group 30015 Mar 1 2004 flute.ps -rwxr-xr-x 1 alex group 485 Oct 21 08:03 floor*
You can remove an alias with the unalias builtin. When the zap alias is removed, it is no longer displayed with the alias builtin and its subsequent use results in an error message:
$ unalias zap $ alias alias ll='ls -l' alias l='ls -ltr' alias ls='ls -F' $ zap maptax.icn bash: zap: command not found
|< Day Day Up >|