using aliases in Linux shell commands
An alias is a (usually short) name that the shell
translates into another (usually longer) name or 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
alias [name[=value]]
Under tcsh the syntax is
alias [name[ value]]
In the bash syntax no spaces are permitted around the equal sign. If valuecontains
spaces or tabs, you must enclose value within quotation
marks. Unlike aliases under tcsh, a bash alias does not accept an argument from
the command line in value. Use a bash function 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). Use the unalias builtin to remove
an alias. When you give an aliasbuiltin command
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'
To view the alias for a particular name, enter the command alias followed
by the name of the alias. Most Linux distributions define at least some
aliases. Enter analias 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. Max 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 dirAcommand displays the dirA alias
and shows that the substitution has already taken place:
$ echo $PWD
/home/max
$ alias
dirA="echo Working directory is $PWD"
$ alias
dirA
alias
dirA='echo Working directory is /home/max'
WHAT READERS
LIKE
·
·
·
When Max creates the dirB alias, he uses single
quotation marks, which prevent the shell from expanding the $PWDvariable.
The alias dirB command shows that the dirBalias
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,
Max uses cd to make cars his working
directory and gives each of the aliases as a command. The alias he created
using double quotation marks displays the name of the directory he created the
alias in as the working directory (which is wrong). In contrast, the dirB alias
displays the proper name of the working directory:
$ cd cars
$ dirA
Working
directory is /home/max
$ dirB
Working
directory is /home/max/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:
·
VIDEO/WEBCAST
Sponsored
Sponsored
·
Go
$ 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
-rw-r-----.
1 max pubs 3089 02-11 16:24 XTerm.ad
-rw-r--r--.
1 max pubs 30015 03-01 14:24 flute.ps
-rw-r--r--.
1 max pubs 641 04-01 08:12 fixtax.icn
-rw-r--r--.
1 max pubs 484 04-09 08:14 maptax.icn
drwxrwxr-x.
2 max pubs 1024 08-09 17:41 Tiger
drwxrwxr-x.
2 max pubs 1024 09-10 11:32 testdir
-rwxr-xr-x.
1 max pubs 485 09-21 08:03 floor
drwxrwxr-x.
2 max pubs 1024 09-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 enter 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, thereby helping
you avoid 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 each time you use ls.The -F option causesls to print a slash (/) at
the end of directory names and an asterisk (*) at the end of the names
of executable files.
$ alias ls='ls -F'
$ alias
ll='ls -l'
$ ll
drwxrwxr-x.
2 max pubs 1024 09-27 20:19 Test_Emacs/
drwxrwxr-x.
2 max pubs 1024 08-09 17:41 Tiger/
-rw-r-----.
1 max pubs 3089 02-11 16:24 XTerm.ad
-rw-r--r--.
1 max pubs 641 04-01 08:12 fixtax.icn
-rw-r--r--.
1 max pubs 30015 03-01 14:24 flute.ps
-rwxr-xr-x.
1 max pubs 485 09-21 08:03 floor*
-rw-r--r--.
1 max pubs 484 04-09 08:14 maptax.icn
drwxrwxr-x.
2 max pubs 1024 09-10 11:32 testdir/
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 displays 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 is executed:
$ ll f*
-rw-r--r--.
1 max pubs 641 04-01 08:12 fixtax.icn
-rw-r--r--.
1 max pubs 30015 03-01 14:24 flute.ps
-rwxr-xr-x.
1 max pubs 485 09-21 08:03 floor*
You can remove an alias using the unalias builtin. When
the zap alias is removed, it is no longer displayed by 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
No comments:
Post a Comment