Debian Codegraph @

HTML/aliases.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Aliases</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Advanced Topics"
15 HREF="part5.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Recursion Without Local Variables"
18 HREF="recurnolocvar.html"><LINK
19 REL="NEXT"
20 TITLE="List Constructs"
21 HREF="list-cons.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="CHAPTER"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="recurnolocvar.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="list-cons.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="ALIASES"
95 ></A
96 >Chapter 25. Aliases</H1
97 ><P
98 ><A
99 NAME="ALIASREF"
100 ></A
101 ></P
102 ><P
103 >A Bash <I
104 CLASS="FIRSTTERM"
105 >alias</I
106 > is essentially nothing
107 more than a keyboard shortcut, an abbreviation, a means of
108 avoiding typing a long command sequence. If, for example,
109 we include <B
110 CLASS="COMMAND"
111 >alias lm="ls -l | more"</B
112 > in
113 the <A
114 HREF="sample-bashrc.html"
115 ><TT
116 CLASS="FILENAME"
117 >~/.bashrc</TT
118 >
119 file</A
120 >, then each <TT
121 CLASS="USERINPUT"
122 ><B
123 >lm</B
124 ></TT
125 >
126 <A
127 NAME="AEN18669"
128 HREF="#FTN.AEN18669"
129 >[1]</A
130 >
131 typed at the command-line will automatically be replaced by a
132 <B
133 CLASS="COMMAND"
134 >ls -l | more</B
135 >. This can save a great deal of
136 typing at the command-line and avoid having to remember complex
137 combinations of commands and options. Setting <B
138 CLASS="COMMAND"
139 >alias
140 rm="rm -i"</B
141 > (interactive mode delete) may save a
142 good deal of grief, since it can prevent inadvertently deleting
143 important files.</P
144 ><P
145 >In a script, aliases have very limited usefulness. It would be
146 nice if aliases could assume some of the functionality of
147 the <B
148 CLASS="COMMAND"
149 >C</B
150 > preprocessor, such as macro expansion,
151 but unfortunately Bash does not expand arguments within the
152 alias body.
153 <A
154 NAME="AEN18676"
155 HREF="#FTN.AEN18676"
156 >[2]</A
157 >
158 Moreover, a script fails to expand an alias itself
159 within <SPAN
160 CLASS="QUOTE"
161 >"compound constructs,"</SPAN
162 > such as <A
163 HREF="tests.html#IFTHEN"
164 >if/then</A
165 > statements, loops, and
166 functions. An added limitation is that an alias will not expand
167 recursively. Almost invariably, whatever we would like an alias
168 to do could be accomplished much more effectively with a <A
169 HREF="functions.html#FUNCTIONREF"
170 >function</A
171 >.</P
172 ><DIV
173 CLASS="EXAMPLE"
174 ><HR><A
175 NAME="AL"
176 ></A
177 ><P
178 ><B
179 >Example 25-1. Aliases within a script</B
180 ></P
181 ><TABLE
182 BORDER="0"
183 BGCOLOR="#E0E0E0"
184 WIDTH="100%"
185 ><TR
186 ><TD
187 ><PRE
188 CLASS="PROGRAMLISTING"
189 > 1&nbsp;#!/bin/bash
190 2&nbsp;# alias.sh
191 3&nbsp;
192 4&nbsp;shopt -s expand_aliases
193 5&nbsp;# Must set this option, else script will not expand aliases.
194 6&nbsp;
195 7&nbsp;
196 8&nbsp;# First, some fun.
197 9&nbsp;alias Jesse_James='echo "\"Alias Jesse James\" was a 1959 comedy starring Bob Hope."'
198 10&nbsp;Jesse_James
199 11&nbsp;
200 12&nbsp;echo; echo; echo;
201 13&nbsp;
202 14&nbsp;alias ll="ls -l"
203 15&nbsp;# May use either single (') or double (") quotes to define an alias.
204 16&nbsp;
205 17&nbsp;echo "Trying aliased \"ll\":"
206 18&nbsp;ll /usr/X11R6/bin/mk* #* Alias works.
207 19&nbsp;
208 20&nbsp;echo
209 21&nbsp;
210 22&nbsp;directory=/usr/X11R6/bin/
211 23&nbsp;prefix=mk* # See if wild card causes problems.
212 24&nbsp;echo "Variables \"directory\" + \"prefix\" = $directory$prefix"
213 25&nbsp;echo
214 26&nbsp;
215 27&nbsp;alias lll="ls -l $directory$prefix"
216 28&nbsp;
217 29&nbsp;echo "Trying aliased \"lll\":"
218 30&nbsp;lll # Long listing of all files in /usr/X11R6/bin stating with mk.
219 31&nbsp;# An alias can handle concatenated variables -- including wild card -- o.k.
220 32&nbsp;
221 33&nbsp;
222 34&nbsp;
223 35&nbsp;
224 36&nbsp;TRUE=1
225 37&nbsp;
226 38&nbsp;echo
227 39&nbsp;
228 40&nbsp;if [ TRUE ]
229 41&nbsp;then
230 42&nbsp; alias rr="ls -l"
231 43&nbsp; echo "Trying aliased \"rr\" within if/then statement:"
232 44&nbsp; rr /usr/X11R6/bin/mk* #* Error message results!
233 45&nbsp; # Aliases not expanded within compound statements.
234 46&nbsp; echo "However, previously expanded alias still recognized:"
235 47&nbsp; ll /usr/X11R6/bin/mk*
236 48&nbsp;fi
237 49&nbsp;
238 50&nbsp;echo
239 51&nbsp;
240 52&nbsp;count=0
241 53&nbsp;while [ $count -lt 3 ]
242 54&nbsp;do
243 55&nbsp; alias rrr="ls -l"
244 56&nbsp; echo "Trying aliased \"rrr\" within \"while\" loop:"
245 57&nbsp; rrr /usr/X11R6/bin/mk* #* Alias will not expand here either.
246 58&nbsp; # alias.sh: line 57: rrr: command not found
247 59&nbsp; let count+=1
248 60&nbsp;done
249 61&nbsp;
250 62&nbsp;echo; echo
251 63&nbsp;
252 64&nbsp;alias xyz='cat $0' # Script lists itself.
253 65&nbsp; # Note strong quotes.
254 66&nbsp;xyz
255 67&nbsp;# This seems to work,
256 68&nbsp;#+ although the Bash documentation suggests that it shouldn't.
257 69&nbsp;#
258 70&nbsp;# However, as Steve Jacobson points out,
259 71&nbsp;#+ the "$0" parameter expands immediately upon declaration of the alias.
260 72&nbsp;
261 73&nbsp;exit 0</PRE
262 ></TD
263 ></TR
264 ></TABLE
265 ><HR></DIV
266 ><P
267 ><A
268 NAME="UNALIASREF"
269 ></A
270 ></P
271 ><P
272 >The <B
273 CLASS="COMMAND"
274 >unalias</B
275 > command removes a previously
276 set <I
277 CLASS="FIRSTTERM"
278 >alias</I
279 >.</P
280 ><DIV
281 CLASS="EXAMPLE"
282 ><HR><A
283 NAME="UNAL"
284 ></A
285 ><P
286 ><B
287 >Example 25-2. <I
288 CLASS="FIRSTTERM"
289 >unalias</I
290 >: Setting and unsetting
291 an alias</B
292 ></P
293 ><TABLE
294 BORDER="0"
295 BGCOLOR="#E0E0E0"
296 WIDTH="100%"
297 ><TR
298 ><TD
299 ><PRE
300 CLASS="PROGRAMLISTING"
301 > 1&nbsp;#!/bin/bash
302 2&nbsp;# unalias.sh
303 3&nbsp;
304 4&nbsp;shopt -s expand_aliases # Enables alias expansion.
305 5&nbsp;
306 6&nbsp;alias llm='ls -al | more'
307 7&nbsp;llm
308 8&nbsp;
309 9&nbsp;echo
310 10&nbsp;
311 11&nbsp;unalias llm # Unset alias.
312 12&nbsp;llm
313 13&nbsp;# Error message results, since 'llm' no longer recognized.
314 14&nbsp;
315 15&nbsp;exit 0</PRE
316 ></TD
317 ></TR
318 ></TABLE
319 ><HR></DIV
320 ><TABLE
321 BORDER="0"
322 BGCOLOR="#E0E0E0"
323 WIDTH="100%"
324 ><TR
325 ><TD
326 ><PRE
327 CLASS="SCREEN"
328 > <TT
329 CLASS="PROMPT"
330 >bash$ </TT
331 ><TT
332 CLASS="USERINPUT"
333 ><B
334 >./unalias.sh</B
335 ></TT
336 >
337 <TT
338 CLASS="COMPUTEROUTPUT"
339 >total 6
340 drwxrwxr-x 2 bozo bozo 3072 Feb 6 14:04 .
341 drwxr-xr-x 40 bozo bozo 2048 Feb 6 14:04 ..
342 -rwxr-xr-x 1 bozo bozo 199 Feb 6 14:04 unalias.sh
343
344 ./unalias.sh: llm: command not found</TT
345 ></PRE
346 ></TD
347 ></TR
348 ></TABLE
349 ></DIV
350 ><H3
351 CLASS="FOOTNOTES"
352 >Notes</H3
353 ><TABLE
354 BORDER="0"
355 CLASS="FOOTNOTES"
356 WIDTH="100%"
357 ><TR
358 ><TD
359 ALIGN="LEFT"
360 VALIGN="TOP"
361 WIDTH="5%"
362 ><A
363 NAME="FTN.AEN18669"
364 HREF="aliases.html#AEN18669"
365 >[1]</A
366 ></TD
367 ><TD
368 ALIGN="LEFT"
369 VALIGN="TOP"
370 WIDTH="95%"
371 ><P
372 > ... as the first word of a command string.
373 Obviously, an alias is only meaningful at the
374 <SPAN
375 CLASS="emphasis"
376 ><I
377 CLASS="EMPHASIS"
378 >beginning</I
379 ></SPAN
380 > of a command.</P
381 ></TD
382 ></TR
383 ><TR
384 ><TD
385 ALIGN="LEFT"
386 VALIGN="TOP"
387 WIDTH="5%"
388 ><A
389 NAME="FTN.AEN18676"
390 HREF="aliases.html#AEN18676"
391 >[2]</A
392 ></TD
393 ><TD
394 ALIGN="LEFT"
395 VALIGN="TOP"
396 WIDTH="95%"
397 ><P
398 >However, aliases do seem to expand positional
399 parameters.</P
400 ></TD
401 ></TR
402 ></TABLE
403 ><DIV
404 CLASS="NAVFOOTER"
405 ><HR
406 ALIGN="LEFT"
407 WIDTH="100%"><TABLE
408 SUMMARY="Footer navigation table"
409 WIDTH="100%"
410 BORDER="0"
411 CELLPADDING="0"
412 CELLSPACING="0"
413 ><TR
414 ><TD
415 WIDTH="33%"
416 ALIGN="left"
417 VALIGN="top"
418 ><A
419 HREF="recurnolocvar.html"
420 ACCESSKEY="P"
421 >Prev</A
422 ></TD
423 ><TD
424 WIDTH="34%"
425 ALIGN="center"
426 VALIGN="top"
427 ><A
428 HREF="index.html"
429 ACCESSKEY="H"
430 >Home</A
431 ></TD
432 ><TD
433 WIDTH="33%"
434 ALIGN="right"
435 VALIGN="top"
436 ><A
437 HREF="list-cons.html"
438 ACCESSKEY="N"
439 >Next</A
440 ></TD
441 ></TR
442 ><TR
443 ><TD
444 WIDTH="33%"
445 ALIGN="left"
446 VALIGN="top"
447 >Recursion Without Local Variables</TD
448 ><TD
449 WIDTH="34%"
450 ALIGN="center"
451 VALIGN="top"
452 ><A
453 HREF="part5.html"
454 ACCESSKEY="U"
455 >Up</A
456 ></TD
457 ><TD
458 WIDTH="33%"
459 ALIGN="right"
460 VALIGN="top"
461 >List Constructs</TD
462 ></TR
463 ></TABLE
464 ></DIV
465 ></BODY
466 ></HTML
467 >