|
|
A command alias is a command you type that stands for a longer, or harder-to-remember, command line. For example, if you are a DOS user, you might create an alias called dir instead of trying to remember the ls command.
The way you create aliases depends on the shell you are using. In the Bourne shell, you need to set up a shell function, while in the Korn shell and the C shell, you can use the built-in alias command.
To set up an alias in sh, add the following lines to your .profile:
aliasname() { command }Here aliasname is the name you want to call the alias and command is the command you want to alias. When you choose a name for your alias, be careful to choose a name that is not already the name of a UNIX command, otherwise, when you type the name of your alias, the UNIX system may think you mean the command of the same name instead.
For example, to create an alias called dir in sh that shows you a file listing, add the following lines to your .profile:
dir() { ls }You can make an alias that uses a filename as an argument, but you need to tell your shell where to insert the filename. You can do this by using the variable $1, which the shell reads as ``insert the first argument here.'' For example, if you want to create an alias in sh called print that runs a file through the pr (``pretty print'') command before sending it to the lineprinter, you could set up the following function:
print() { pr $1 | lp }To print a file using your new alias, you would type print file where file is the name of the file you want printed.
Aliases in the Korn and C shells are introduced by the built-in shell command alias. Aliases in the Korn shell have the following format:
alias aliasname='command'So, the dir alias would be:
alias dir='ls'And the print alias would be:
alias print='pr $1 | lp'Aliases in the C shell have this format:
alias aliasname 'command'The dir alias in csh would be:
alias dir 'ls'And the print alias in csh would be:
alias print 'pr -n :* | lp'