.TI CSHELL/ALIASES Renaming and Abbreviating Commands [DRAFT] Aliases provide a means of setting up a name of your choosing to refer to a command or sequence of commands. When you use that name as if it were a command, the C shell replaces it with the command sequence to which it refers. Aliases effectively allow you to abbreviate commands and to alter the meaning of existing commands, although only from your point of view. You set up an alias with the alias command \fBalias\fP \fIname\fP \fItext\fP where \fIname\fP is the name you choose and \fItext\fP is the replacing text of the command or commands to which \fIname\fP will refer. For example, the aliases .IP .nf % alias pe printenv % alias nr nroff -ms -Tdtc -s1 .LP would cause the shell to replace your subsequent command "pe" with "printenv" before executing it, and similarly for "nr". The actual replacements are shown below for two sample commands. The first command is what causes the shell to print the result of the replacement just before executing it. .IP .nf % set echo % pe TERM printenv TERM % nr chap1 chap2 > out nroff -ms -Tdtc -s1 chap1 chap2 > out .LP Notice that when you use an alias, whatever you entered on the rest of the line stays on the end after the replacement. This will not be the case when special alias arguments are used, but that is explained later. One of the trickiest parts about aliases is quoting. For instance the quotes in .IP % alias doit 'tbl chap1 chap2 | nroff -ms -Tlpr | lpr' .LP are needed because without them the shell would have perceived three command instead of one. The following alias lists the files on the hypothetical gumball machine after printing the name of the remote directory first; it has more subtle quoting problems: .IP % alias rlist 'rsh gumball "echo $cwd ; ls" | more' .LP The outermost quotes achieve the same effect as in the previous example, and they have to differ from the inner quotes to be interpreted correctly. Unfortunately, the variable $cwd will always be replaced with the current directory on the local machine because the C shell insists on doing variable substitution within enclosing " marks. Thus even inverting the use of " and ' above would not work. Several acceptable ways are .IP .nf % alias rlist "rsh gumball 'echo" '$cwd' "; ls' | more" % alias rlist 'rsh gumball echo \\$cwd \\; ls | more' % alias rlist rsh gumball echo \\\\\\$cwd \\\\\\; ls \\| more' .LP and no one can say which one is best. Since it is possible to choose an alias name that is currently the name of a normal system command, you can effectively replace or remove a system command. For example, .IP .nf % alias rm echo Sorry, cannot remove % rm thesis Sorry, cannot remove thesis % alias troff /usr/old/troff .LP You may want to know the aliases currently in effect, and can always find out by entering the alias command with no argument. If you enter it with one argument, the shell prints the the replacing text for the argument, if any. There is also room for deviant or whimsical uses of aliases. .IP .nf % alias why echo BECAUSE % why BECAUSE % alias vi x % alias x y % alias y echo Bus error -- core dumped % vi myfile Bus error -- core dumped myfile % alias a alias % a a alias vi x why (echo BECAUSE) x y y (echo Bus error -- core dumped) .LP Aliases can also work like simple, fast shell scripts with arguments; simple because only one-line commands work, and fast because no new process is involved and no extra file has to be accessed (as with the source command). Unfortunately, the mechanism for accessing arguments involves complicated history-like substitutions and difficult quoting problems. When you use an alias, any arguments you give to it are usually tacked on to the end of the replacing text when the shell does the replacement. This practice is suspended completely if your replacing text contains what appear to be history substitutions. Instead, the C shell selectively places the arguments into the replacing text based on the history constructs you used to define the alias. Here is how it works. When the shell replaces the command you typed in with the text of the alias, it treats your original command as if it were the very last command. Thus, for example, !$ inside the definition of your alias would be replaced by the last argument with which the alias was invoked. In the alias .IP .nf % alias whois 'grep \\!:1 /etc/passwd \\!:2*' % set echo % whois fred > junk grep fred /etc/passwd > junk .LP the \\!:1 designates the first argument and \\!:2* designates all the rest. Remember that nothing but \\ will quote !. Here is an alias which lets you do arithmetic in the C shell: .IP .nf % alias val '@ z = (\\!*) ; echo $z' % val 3 - 2 * 6 -9 .LP Although aliases have many interesting properties, you may find them difficult to use and easy to get along without. On that note, here are some aliases that might appear in a real .cshrc file, which can set up aliases for you automatically when you login. .nf alias h history -r +20 alias j jobs -l alias m more alias q exit alias z suspend set whoami = `hostname | sed s/ucb//` alias cd 'chdir \\!* ; set prompt = "$whoami $cwd:t % "' cd alias la 'set q=$whoami ; if (\\!$ != la) set q=\\!$ ; ruptime | fgrep $q' alias ll ls -l alias lld ls -ld alias pd 'pushd \\!:* ; set prompt = "$whoami $cwd:t % "' alias pe printenv alias so source alias ts 'set noglob ; eval `tset -s \\!*`'; alias pop 'popd \\!:* ; set prompt = "$whoami $cwd:t % "' alias grab 'sed "/^From: .*\\!:1/,/^From/\\\\!d" /usr/spool/mail/jak \\!:2*' alias whois 'grep \\!:1 /etc/passwd \\!:2*' alias alarm 'set mail = (5 \\!* $mail)' alias hic 'if ($i == $whoami) continue ; echo $i' alias ismail '@ q = 1 + (-z /usr/spool/mail/\\!^) + (-e /usr/spool/mail/\\!^) ; \\ set s = ("No mail file" Yep Nope) ; echo $s[$q]' alias slpr "sed 's/^/ /' \\!* | lpr" alias val "echo 'scale=5 ; \\!*:x' | bc" alias Troff troff -Q -Tcat alias Vroff troff -Q -Tversatec alias Iroff troff -Q -T6670 jak