|
|
1.1 root 1: .TI CSHELL/ALIASES
2: Renaming and Abbreviating Commands [DRAFT]
3:
4:
5: Aliases provide a means of setting up a name of your choosing
6: to refer to a command or sequence of commands.
7: When you use that name as if it were a command,
8: the C shell replaces it with the command sequence to which it refers.
9: Aliases effectively allow you to abbreviate commands
10: and to alter the meaning of existing commands,
11: although only from your point of view.
12:
13: You set up an alias with the alias command
14:
15: \fBalias\fP \fIname\fP \fItext\fP
16:
17: where \fIname\fP is the name you choose and \fItext\fP
18: is the replacing text of the command or commands to which
19: \fIname\fP will refer.
20: For example, the aliases
21: .IP
22: .nf
23: % alias pe printenv
24: % alias nr nroff -ms -Tdtc -s1
25: .LP
26: would cause the shell to replace your subsequent command "pe"
27: with "printenv" before executing it, and similarly for "nr".
28: The actual replacements are shown below for two sample commands.
29: The first command is what causes the shell to print the result
30: of the replacement just before executing it.
31: .IP
32: .nf
33: % set echo
34: % pe TERM
35: printenv TERM
36: % nr chap1 chap2 > out
37: nroff -ms -Tdtc -s1 chap1 chap2 > out
38: .LP
39: Notice that when you use an alias, whatever you entered on the rest
40: of the line stays on the end after the replacement.
41: This will not be the case when special alias arguments are used,
42: but that is explained later.
43:
44: One of the trickiest parts about aliases is quoting.
45: For instance the quotes in
46: .IP
47: % alias doit 'tbl chap1 chap2 | nroff -ms -Tlpr | lpr'
48: .LP
49: are needed because without them the shell would have
50: perceived three command instead of one.
51: The following alias lists the files on the hypothetical
52: gumball machine after printing the name of the remote directory first;
53: it has more subtle quoting problems:
54: .IP
55: % alias rlist 'rsh gumball "echo $cwd ; ls" | more'
56: .LP
57: The outermost quotes achieve the same effect as in the
58: previous example, and they have to differ from the
59: inner quotes to be interpreted correctly.
60: Unfortunately, the variable $cwd will always be replaced with
61: the current directory on the local machine because the
62: C shell insists on doing variable
63: substitution within enclosing " marks.
64: Thus even inverting the use of " and ' above would not work.
65: Several acceptable ways are
66: .IP
67: .nf
68: % alias rlist "rsh gumball 'echo" '$cwd' "; ls' | more"
69: % alias rlist 'rsh gumball echo \\$cwd \\; ls | more'
70: % alias rlist rsh gumball echo \\\\\\$cwd \\\\\\; ls \\| more'
71: .LP
72: and no one can say which one is best.
73:
74: Since it is possible to choose an alias name that is currently
75: the name of a normal system command, you can effectively replace or
76: remove a system command.
77: For example,
78: .IP
79: .nf
80: % alias rm echo Sorry, cannot remove
81: % rm thesis
82: Sorry, cannot remove thesis
83: % alias troff /usr/old/troff
84: .LP
85: You may want to know the aliases currently in effect,
86: and can always find out by entering the alias command with
87: no argument.
88: If you enter it with one argument, the shell prints the
89: the replacing text for the argument, if any.
90:
91: There is also room for deviant or whimsical uses of aliases.
92: .IP
93: .nf
94: % alias why echo BECAUSE
95: % why
96: BECAUSE
97: % alias vi x
98: % alias x y
99: % alias y echo Bus error -- core dumped
100: % vi myfile
101: Bus error -- core dumped myfile
102: % alias a alias
103: % a
104: a alias
105: vi x
106: why (echo BECAUSE)
107: x y
108: y (echo Bus error -- core dumped)
109: .LP
110: Aliases can also work like simple, fast shell scripts with arguments;
111: simple because only one-line commands work, and fast because
112: no new process is involved and no extra file has to be accessed
113: (as with the source command).
114: Unfortunately, the mechanism for accessing arguments
115: involves complicated history-like
116: substitutions and difficult quoting problems.
117:
118: When you use an alias, any arguments you give to it
119: are usually tacked on to the end of the replacing text when
120: the shell does the replacement.
121: This practice is suspended completely if your replacing
122: text contains what appear to be history substitutions.
123: Instead, the C shell selectively places the arguments
124: into the replacing text based on the history constructs
125: you used to define the alias.
126:
127: Here is how it works.
128: When the shell replaces the command you typed in with the text
129: of the alias, it treats your original command as if it were the
130: very last command.
131: Thus, for example, !$ inside the definition
132: of your alias would be replaced by the last argument with which
133: the alias was invoked.
134: In the alias
135: .IP
136: .nf
137: % alias whois 'grep \\!:1 /etc/passwd \\!:2*'
138: % set echo
139: % whois fred > junk
140: grep fred /etc/passwd > junk
141: .LP
142: the \\!:1 designates the first argument and \\!:2* designates
143: all the rest.
144: Remember that nothing but \\ will quote !.
145: Here is an alias which lets you do arithmetic in the C shell:
146: .IP
147: .nf
148: % alias val '@ z = (\\!*) ; echo $z'
149: % val 3 - 2 * 6
150: -9
151: .LP
152: Although aliases have many interesting properties,
153: you may find them difficult to use and easy to get along without.
154: On that note, here are some aliases that might appear
155: in a real .cshrc file, which can set up aliases for you
156: automatically when you login.
157:
158: .nf
159: alias h history -r +20
160: alias j jobs -l
161: alias m more
162: alias q exit
163: alias z suspend
164: set whoami = `hostname | sed s/ucb//`
165: alias cd 'chdir \\!* ; set prompt = "$whoami $cwd:t % "'
166: cd
167: alias la 'set q=$whoami ; if (\\!$ != la) set q=\\!$ ; ruptime | fgrep $q'
168: alias ll ls -l
169: alias lld ls -ld
170: alias pd 'pushd \\!:* ; set prompt = "$whoami $cwd:t % "'
171: alias pe printenv
172: alias so source
173: alias ts 'set noglob ; eval `tset -s \\!*`';
174: alias pop 'popd \\!:* ; set prompt = "$whoami $cwd:t % "'
175: alias grab 'sed "/^From: .*\\!:1/,/^From/\\\\!d" /usr/spool/mail/jak \\!:2*'
176: alias whois 'grep \\!:1 /etc/passwd \\!:2*'
177: alias alarm 'set mail = (5 \\!* $mail)'
178: alias hic 'if ($i == $whoami) continue ; echo $i'
179: alias ismail '@ q = 1 + (-z /usr/spool/mail/\\!^) + (-e /usr/spool/mail/\\!^) ; \\
180: set s = ("No mail file" Yep Nope) ; echo $s[$q]'
181: alias slpr "sed 's/^/ /' \\!* | lpr"
182: alias val "echo 'scale=5 ; \\!*:x' | bc"
183: alias Troff troff -Q -Tcat
184: alias Vroff troff -Q -Tversatec
185: alias Iroff troff -Q -T6670
186:
187:
188: jak
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.