next up previous contents
Next: Programming in a UNIX Up: UNIX Previous: xv - Display graphics   Contents

Aliases

Aliases allow you to customise the commands you type to the shell. For example, suppose you can never remember that, to type out a file, you must use e.g.:-

cat my_file
and wish you could instead use:-
type my_file
You can do this is you set up an alias that translates the character string ``type'' into ``cat'' by issuing the command:-
alias type cat
People transferring to UNIX from another system frequently set up aliases like this that translate that system's commands into the corresponding UNIX ones so that they feel more at home. If you will be a casual and infrequent UNIX user this can be a good idea. However, if you intend to use UNIX a lot then it is far better to make the effort to learn the commands; setting up the aliases won't change the fact that you are using UNIX, and you may have difficulties collaborating with others who don't use your customisation. Also, aliases are a C shell feature; you cannot use the Bourne shell.

Another reason to use aliases is to add default flags. For example, just think what damage these 4 characters can do:-

rm *
it is far safer to type:-
rm -i *
so that you at least get a chance to change your mind! However, you have to remember to do that, unless you set up the alias:-
alias rm rm -i
now, each type you type rm it gets replaced with the same command but with a -i added. This demonstrates that the character string that replaces the alias can contain spaces. Actually, this alias is so useful, it may have been set up for you, see 6.2.6.

So now you have a safety line for the rm command, which is fine until the day comes when you create a temporary file with thousands of files and want to delete them all. Having checked that you have got the directory right (!), you want to remove the -i option. To do this you could can cancel the alias with:-

unalias rm
but then you have to reinstate it afterwards. If you really only want to remove the alias for one command you can bypass alias substituting by adding a \ before the command e.g.:-
\rm *
which gets you back to the raw command again.

You can see if a command is defined as an alias by using the alias command without the replacement string, e.g.:-

alias rm
or, to see all the aliases currently in force, just:-
alias

Aliases remain in force until you log out unless you unalias them. However if you execute a command script, see section 6.2, the script runs as a separate process and won't have these aliases. Typically, if you set up an alias, you will want it set each time you log in and also be available for any script file you execute. To do this, you should add the alias commands to your .cshrc file, see section 6.2.6


next up previous contents
Next: Programming in a UNIX Up: UNIX Previous: xv - Display graphics   Contents
P.D. Gronbech (IT Staff) 2015-10-02