You use the
alias command to create another name for a command. The
alias command operates like a macro that expands to the command it represents. The alias does not literally replace the name of the command; it simply gives another name to that command. An
alias command begins with the keyword
alias and the new name for the command, followed by an equal sign and the command the alias will reference.
Note |
No spaces can be around the equal sign used in the alias command. |
In the next example,
list becomes another name for the
$ alias list=ls $ ls mydata today $ list mydata today $
You can also use an alias to substitute for a command and its option, but you need to enclose both the command and the option within single quotes. Any command you alias that contains spaces must be enclosed in single quotes. In the next example, the alias
lss references the
ls command with its
-s option, and the alias
lsa references the
ls command with the
-F option.
ls with the
-s option lists files and their sizes in blocks, and the
ls with the
-F option places a slash after directory names. Notice how single quotes enclose the command and its option.
$ alias lss='ls -s' $ lss mydata 14 today 6 reports 1 $ alias lsa='ls -F' $ lsa mydata today reports/ $
Aliases are helpful for simplifying complex operations. In the next example,
listlong becomes another name for the
ls command, with the
-l option for the long format listing all file information, as well as the
-h option for using human readable format for file sizes. Be sure to encase the command and its arguments within single quotes so they are taken as one argument and not parsed by the shell.
$ alias listlong='ls -lh' $ listlong -rw-r--r-- 1 root root 51K Sep 18 2003 mydata -rw-r--r-- 1 root root 16K Sep 27 2003 today
You may often use an alias to include a command name with an argument. If you execute a command that has an argument with a complex combination of special characters on a regular basis, you may want to alias it. For example, suppose you often list just your source code and object code files—those files ending in either a .c or .o. You would need to use as an argument for
ls a combination of special characters such as
*.[co] . Instead, you could alias
ls with the
.[co] argument, giving it a simple name. In the next example, the user creates an alias called
lsc for the command
ls.[co] :
$ alias lsc='ls *.[co]' $ lsc main.c main.o lib.c lib.o
You can also use the name of a command as an alias. This can be helpful in cases where you should use a command only with a specific option. In the case of the
rm ,
cp , and
mv commands, the
-i option should always be used to ensure an existing file is not overwritten. Instead of constantly being careful to use the
-i option each time you use one of these commands, the command name can be aliased to include the option. In the next example, the
rm ,
cp , and
mv commands have been aliased to include the
-i option:
$ alias rm='rm -i' $ alias mv='mv -i' $ alias cp='cp -i'
The
alias command by itself provides a list of all aliases that have been defined, showing the commands they represent. You can remove an alias by using the
unalias command. In the next example, the user lists the current aliases and then removes the
lsa alias:
$ alias lsa=ls -F list=ls rm=rm -i $ unalias lsa