(
UN*X shells:)
An alias is a renaming of a command by the shell you're using. Bourne shell (sh) doesn't have aliases; csh and its derivative tcsh, as well as bash, zsh and a gazillion other shells all support aliases.
Simple aliases just call a command by another, possibly more familiar, name. For years, if you were new in UN*Xland from DOSland, you were expected to announce this fact by saying
alias dir ls
alias DIR ls
alias type cat
alias TYPE cat
in your
.cshrc (you probably weren't told about other shells, so I'll not cover their peculiar
syntax here). This supposedly made you feel more at home by letting you type
TYPE filename.txt instead of
cat filename.txt. Note, however, that it's still
case sensitive, and that it probably doesn't look like DOS. Such aliases are still useful, however, to shorten long commands.
csh-style aliases
These take the form
alias short long
where each of
short and
long is a single "shell word" (in particular, you probably want to
quote long!).
If long looks "normal", then short arg1 arg2... will be treated as long arg1 arg2. So you can say
alias print 'lpr -Pmyprinter'
and it will work.
If long contains csh history expansion tokens, then short arg1 arg2... will be replaced with long, after history expansion is performed on it as if short arg1 arg2... were the previous line of input! Here are 2 examples from the manual:
alias lookup 'grep \!^ /etc/passwd'
lookup bill
expands to
grep bill /etc/passwd;
alias print `pr \!* | lpr
print file1.c file1.h file2.c
runs
pr on
file1.c file1.h file2.c and sense the result off to lpr.
Thanks to the horrible syntax of csh, it is hard to think of something more awkward than writing a good alias there. But we still do it.
zsh-style aliases
These use the different syntax
alias short=long
just to annoy you. The -g option allows you to set a
global alias, which expands anywhere in the command line. Alias expansions ending in a
space trigger attempted expansion of the
next word after.
Despite all this, this style of aliases is actually simpler than csh's: you cannot manipulate the words of the command line. The availability of functions at the shell level means you can use aliases just for simpler purposes, where they are more handy; use functions when you need to process the parameters.
Other Bourne-shell derived shells generally have similar aliasing mechanisms.