Commit | Line | Data |
---|---|---|
b7aa7294 KB |
1 | .\" Copyright (c) 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
65622797 KB |
3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" Kenneth Almquist. | |
6 | .\" | |
7 | .\" %sccs.include.redist.man% | |
8 | .\" | |
34a1a05a | 9 | .\" @(#)sh.1 8.6 (Berkeley) %G% |
65622797 | 10 | .\" |
7ab274c2 | 11 | .na |
dd00107c MT |
12 | .TH SH 1 |
13 | .SH NAME | |
376e2f43 | 14 | sh \- command interpreter (shell) |
dd00107c | 15 | .SH SYNOPSIS |
376e2f43 | 16 | sh [-/+aCefnuvxIimsVEb] [-/+o longname] [arg ...] |
dd00107c MT |
17 | .SH DESCRIPTION |
18 | .LP | |
af143eb3 KM |
19 | Sh is the standard command interpreter for the system. |
20 | The current version of sh is in the process of being changed to | |
21 | conform with the POSIX 1003.2 and 1003.2a specifications for | |
dd00107c MT |
22 | the shell. This version has many features which make it appear |
23 | similar in some respects to the Korn shell, but it is not a Korn | |
24 | shell clone (run GNU's bash if you want that). Only features | |
af143eb3 | 25 | designated by POSIX, plus a few Berkeley extensions, are being |
dd00107c | 26 | incorporated into this shell. We expect POSIX conformance by the |
af143eb3 | 27 | time 4.4 BSD is released. |
dd00107c MT |
28 | This man page is not intended to be a tutorial or a complete |
29 | specification of the shell. | |
30 | .sp 2 | |
31 | .B Overview | |
65622797 | 32 | .sp |
dd00107c MT |
33 | .LP |
34 | The shell is a command that reads lines from | |
af143eb3 KM |
35 | either a file or the terminal, interprets them, and |
36 | generally executes other commands. It is the program that is running | |
dd00107c MT |
37 | when a user logs into the system (although a user can select |
38 | a different shell with the chsh(1) command). | |
af143eb3 | 39 | The shell |
de40fd0b | 40 | implements a language that has flow control constructs, |
dd00107c MT |
41 | a macro facility that provides a variety of features in |
42 | addition to data storage, along with built in history and line | |
43 | editing capabilities. It incorporates many features to | |
44 | aid interactive use and has the advantage that the interpretative | |
45 | language is common to both interactive and non-interactive | |
46 | use (shell scripts). That is, commands can be typed directly | |
7ab274c2 | 47 | to the running shell or can be put into a file and the file |
dd00107c MT |
48 | can be executed directly by the shell. |
49 | .sp 2 | |
50 | .B Invocation | |
65622797 | 51 | .sp |
dd00107c MT |
52 | .LP |
53 | If no args are present and if the standard input of the shell | |
54 | is connected to a terminal (or if the -i flag is set), the shell | |
55 | is considered an interactive shell. An interactive shell | |
56 | generally prompts before each command and handles programming | |
57 | and command errors differently (as described below). | |
58 | When first starting, the shell inspects argument 0, and | |
7ab274c2 | 59 | if it begins with a dash '-', the shell is also considered |
af143eb3 KM |
60 | a login shell. This is normally done automatically by the system |
61 | when the user first logs in. A login shell first reads commands | |
dd00107c MT |
62 | from the files /etc/profile and .profile if they exist. |
63 | If the environment variable ENV is set on entry to a shell, | |
64 | or is set in the .profile of a login shell, the shell next reads | |
65 | commands from the file named in ENV. Therefore, a user should | |
66 | place commands that are to be executed only at login time in | |
67 | the .profile file, and commands that are executed for every | |
68 | shell inside the ENV file. To set the ENV variable to some | |
69 | file, place the following line in your .profile of your home | |
70 | directory | |
71 | .nf | |
72 | ||
73 | ENV=$HOME/.shinit; export ENV | |
74 | ||
75 | .fi | |
7ab274c2 | 76 | substituting for ``.shinit'' any filename you wish. |
dd00107c MT |
77 | Since the ENV file is read for |
78 | every invocation of the shell, including shell scripts and | |
79 | non-interactive shells, the following paradigm is useful | |
80 | for restricting commands in the ENV file to interactive invocations. | |
81 | Place commands within the ``case'' and ``esac'' below (these | |
82 | commands are described later): | |
65622797 | 83 | .nf |
dd00107c MT |
84 | |
85 | case $- in *i*) | |
86 | # commands for interactive use only | |
87 | ... | |
88 | esac | |
89 | ||
65622797 | 90 | .fi |
dd00107c MT |
91 | If command line arguments besides the options have been |
92 | specified, then the shell treats the first argument as the | |
93 | name of a file from which to read commands (a shell script), and | |
94 | the remaining arguments are set as the positional parameters | |
95 | of the shell ($1, $2, etc). Otherwise, the shell reads commands | |
96 | from its standard input. | |
97 | .sp 2 | |
98 | .B Argument List Processing | |
65622797 | 99 | .sp |
dd00107c MT |
100 | .LP |
101 | All of the single letter options have a corresponding name | |
102 | that can be used as an argument to the '-o' option. The | |
af143eb3 | 103 | set -o name is provided next to the single letter option in |
dd00107c | 104 | the description below. |
7ab274c2 MT |
105 | Specifying a dash ``-'' turns the option on, while using a plus ``+'' |
106 | disables the option. | |
dd00107c MT |
107 | The following options can be set from the command line or |
108 | with the set(1) builtin (described later). | |
109 | .TP | |
110 | -a allexport | |
7ab274c2 | 111 | Export all variables assigned to. |
dd00107c MT |
112 | (UNIMPLEMENTED for 4.4alpha) |
113 | .TP | |
114 | -C noclobber | |
51f8b609 | 115 | Don't overwrite existing files with ``>''. |
dd00107c MT |
116 | (UNIMPLEMENTED for 4.4alpha) |
117 | .TP | |
118 | -e errexit | |
51f8b609 | 119 | If not interactive, exit immediately if any |
dd00107c | 120 | untested command fails. |
af143eb3 KM |
121 | The exit status of a command is considered to be |
122 | explicitly tested if the command is used to control | |
123 | an if, elif, while, or until; or if the command is the left | |
7ab274c2 MT |
124 | hand operand of an ``&&'' or ``||'' operator. |
125 | ||
dd00107c MT |
126 | .TP |
127 | -f noglob | |
128 | Disable pathname expansion. | |
129 | .TP | |
130 | -n noexec | |
131 | If not interactive, read commands but do not | |
132 | execute them. This is useful for checking the | |
133 | syntax of shell scripts. | |
134 | .TP | |
135 | -u nounset | |
136 | Write a message to standard error when attempting | |
137 | to expand a variable that is not set, and if the | |
51f8b609 | 138 | shell is not interactive, exit immediately. |
dd00107c MT |
139 | (UNIMPLEMENTED for 4.4alpha) |
140 | .TP | |
141 | -v verbose | |
142 | The shell writes its input to standard error | |
143 | as it is read. Useful for debugging. | |
144 | .TP | |
145 | -x xtrace | |
7ab274c2 MT |
146 | Write each command to standard error (preceded |
147 | by a '+ ') before it is executed. Useful for | |
dd00107c MT |
148 | debugging. |
149 | .TP | |
150 | -I ignoreeof | |
151 | Ignore EOF's from input when interactive. | |
152 | .TP | |
153 | -i interactive | |
154 | Force the shell to behave interactively. | |
155 | .TP | |
156 | -m monitor | |
157 | Turn on job control (set automatically when | |
158 | interactive). | |
159 | .TP | |
160 | -s stdin | |
161 | Read commands from standard input (set automatically | |
162 | if no file arguments are present). This option has | |
163 | no effect when set after the shell has already started | |
164 | running (i.e. with set(1)). | |
165 | .TP | |
166 | -V vi | |
167 | Enable the builtin vi(1) command line editor (disables | |
168 | -E if it has been set). | |
169 | .TP | |
170 | -E emacs | |
171 | Enable the builtin emacs(1) command line editor (disables | |
172 | -V if it has been set). | |
173 | .TP | |
174 | -b notify | |
af143eb3 | 175 | Enable asynchronous notification of background job |
dd00107c MT |
176 | completion. |
177 | (UNIMPLEMENTED for 4.4alpha) | |
178 | .LP | |
179 | .sp 2 | |
180 | .B Lexical Structure | |
181 | .sp | |
182 | .LP | |
af143eb3 | 183 | The shell reads input in terms of lines from a file and breaks |
7ab274c2 MT |
184 | it up into words at whitespace (blanks and tabs), and at |
185 | certain sequences of | |
dd00107c MT |
186 | characters that are special to the shell called ``operators''. |
187 | There are two types of operators: control operators and | |
188 | redirection operators (their meaning is discussed later). | |
189 | Following is a list of operators: | |
190 | .nf | |
191 | .sp | |
192 | Control operators: & && ( ) ; ;; | || <newline> | |
af143eb3 | 193 | .sp |
dd00107c MT |
194 | Redirection operator: < > >| << >> <& >& <<- <> |
195 | .sp | |
196 | .fi | |
197 | .sp 2 | |
198 | .B Quoting | |
199 | .sp | |
200 | .LP | |
201 | Quoting is used to remove the special meaning of certain characters | |
202 | or words to the shell, such as operators, whitespace, or | |
203 | keywords. There are three types of quoting: matched single quotes, | |
204 | matched double quotes, and backslash. | |
205 | .sp 2 | |
206 | .B Backslash | |
207 | .sp | |
208 | .LP | |
209 | A backslash preserves the literal meaning of the following | |
210 | character, with the exception of <newline>. A backslash preceding | |
211 | a <newline> is treated as a line continuation. | |
212 | .sp 2 | |
213 | .B Single Quotes | |
214 | .sp | |
215 | .LP | |
216 | Enclosing characters in single quotes preserves the literal | |
217 | meaning of all the characters. | |
218 | .sp 2 | |
219 | .B Double Quotes | |
220 | .sp | |
221 | .LP | |
222 | Enclosing characters within double quotes preserves the literal | |
223 | meaning of all characters except dollarsign ($), backquote (`), | |
7ab274c2 | 224 | and backslash (\\). The backslash inside double quotes is |
dd00107c | 225 | historically weird, and serves to quote only the following |
7ab274c2 | 226 | characters: $ ` " \\ <newline>. |
dd00107c MT |
227 | Otherwise it remains literal. |
228 | .sp 2 | |
229 | .B Reserved Words | |
230 | .sp | |
231 | .LP | |
232 | Reserved words are words that have special meaning to the | |
af143eb3 | 233 | shell and are recognized at the beginning of a line and |
dd00107c MT |
234 | after a control operator. The following are reserved words: |
235 | .nf | |
236 | ||
237 | ! elif fi while case | |
238 | else for then { } | |
239 | do done until if esac | |
240 | ||
241 | .fi | |
7ab274c2 | 242 | Their meaning is discussed later. |
dd00107c MT |
243 | .sp 2 |
244 | .B Aliases | |
245 | .sp | |
246 | .LP | |
7ab274c2 | 247 | An alias is a name and corresponding value set using the alias(1) |
dd00107c MT |
248 | builtin command. Whenever a reserved word may occur (see above), |
249 | and after checking for reserved words, the shell | |
250 | checks the word to see if it matches an alias. If it does, | |
251 | it replaces it in the input stream with its value. For example, | |
252 | if there is an alias called ``lf'' with the value ``ls -F'', | |
253 | then the input | |
254 | .nf | |
255 | ||
256 | lf foobar <return> | |
257 | ||
258 | would become | |
259 | ||
260 | ls -F foobar <return> | |
261 | ||
262 | .fi | |
263 | .LP | |
264 | Aliases provide a convenient way for naive users to | |
265 | create shorthands for commands without having to learn how | |
266 | to create functions with arguments. They can also be | |
267 | used to create lexically obscure code. This use is discouraged. | |
268 | .sp 2 | |
269 | .B Commands | |
270 | .sp | |
271 | .LP | |
af143eb3 | 272 | The shell interprets the words it reads according to a |
dd00107c MT |
273 | language, the specification of which is outside the scope |
274 | of this man page (refer to the BNF in the POSIX 1003.2 | |
275 | document). Essentially though, a line is read and if | |
276 | the first word of the line (or after a control operator) | |
277 | is not a reserved word, then the shell has recognized a | |
278 | simple command. Otherwise, a complex command or some | |
279 | other special construct may have been recognized. | |
280 | .sp 2 | |
281 | .B Simple Commands | |
282 | .sp | |
283 | .LP | |
284 | If a simple command has been recognized, the shell performs | |
285 | the following actions: | |
286 | .sp | |
287 | 1) Leading words of the form ``name=value'' are | |
288 | stripped off and assigned to the environment of | |
289 | the simple command. Redirection operators and | |
290 | their arguments (as described below) are stripped | |
291 | off and saved for processing. | |
292 | .sp | |
293 | 2) The remaining words are expanded as described in | |
294 | the section called ``Expansions'', and the | |
295 | first remaining word is considered the command | |
296 | name and the command is located. The remaining | |
297 | words are considered the arguments of the command. | |
298 | If no command name resulted, then the ``name=value'' | |
299 | variable assignments recognized in 1) affect the | |
300 | current shell. | |
301 | .sp | |
302 | 3) Redirections are performed as described in | |
303 | the next section. | |
304 | .sp 2 | |
305 | .B Redirections | |
306 | .sp | |
307 | .LP | |
308 | Redirections are used to change where a command reads its input | |
309 | or sends its output. In general, redirections open, close, or | |
310 | duplicate an existing reference to a file. The overall format | |
311 | used for redirection is: | |
312 | .nf | |
313 | ||
314 | [n] redir-op file | |
315 | ||
316 | .fi | |
7ab274c2 MT |
317 | where redir-op is one of the redirection operators mentioned |
318 | previously. Following is a list of the possible redirections. | |
dd00107c MT |
319 | The [n] is an optional number, as in '3' (not '[3]'), that |
320 | refers to a file descriptor. | |
321 | .TP | |
322 | [n]> file | |
323 | Redirect standard output (or n) to file. | |
324 | .TP | |
325 | [n]>| file | |
326 | Same, but override the -C option. | |
327 | .TP | |
328 | [n]>> file | |
329 | Append standard output (or n) to file. | |
330 | .TP | |
331 | [n]< file | |
332 | Redirect standard input (or n) from file. | |
333 | .TP | |
334 | [n1]<&n2 | |
335 | Duplicate standard input (or n1) from | |
336 | file descriptor n2. | |
337 | .TP | |
338 | [n]<&- | |
339 | Close standard input (or n). | |
340 | .TP | |
341 | [n1]>&n2 | |
342 | Duplicate standard output (or n) from | |
343 | n2. | |
344 | .TP | |
345 | [n]>&- | |
346 | Close standard output (or n). | |
347 | .TP | |
348 | [n]<> file | |
349 | Open file for reading and writing on | |
af143eb3 | 350 | standard input (or n). |
dd00107c | 351 | .LP |
af143eb3 | 352 | The following redirection is often called a ``here-document''. |
dd00107c MT |
353 | .nf |
354 | ||
355 | [n]<< delimiter | |
356 | here-doc-text... | |
357 | delimiter | |
358 | ||
359 | .fi | |
360 | All the text on successive lines up to the delimiter is | |
361 | saved away and made available to the command on standard | |
362 | input, or file descriptor n if it is specified. If the delimiter | |
363 | as specified on the initial line is quoted, then the here-doc-text | |
364 | is treated literally, otherwise the text is subjected to | |
365 | parameter expansion, command substitution, and arithmetic | |
366 | expansion (as described in the section on ``Expansions''). If | |
367 | the operator is ``<<-'' instead of ``<<'', then leading tabs | |
368 | in the here-doc-text are stripped. | |
369 | .sp 2 | |
370 | .B Search and Execution | |
371 | .sp | |
372 | .LP | |
af143eb3 | 373 | There are three types of commands: shell functions, builtin commands, and normal programs -- and the |
dd00107c MT |
374 | command is searched for (by name) in that order. They |
375 | each are executed in a different way. | |
376 | .LP | |
af143eb3 KM |
377 | When a shell function is executed, all of the shell positional parameters (except $0, which remains unchanged) are |
378 | set to the arguments of the shell function. | |
379 | The variables which are explicitly placed in the environment of | |
380 | the command (by placing assignments to them before the | |
dd00107c | 381 | function name) are made local to the function and are set |
af143eb3 KM |
382 | to the values given. Then the command given in the function |
383 | definition is executed. The positional parameters are | |
384 | restored to their original values when the command completes. | |
dd00107c | 385 | .LP |
7ab274c2 | 386 | Shell builtins are executed internally to the shell, without spawning a new process. |
dd00107c MT |
387 | .LP |
388 | Otherwise, if the command name doesn't match a function | |
389 | or builtin, the command is searched for as a normal | |
390 | program in the filesystem (as described in the next section). | |
af143eb3 KM |
391 | When a normal program is executed, the shell runs the program, |
392 | passing the arguments and the environment to the | |
393 | program. If the program is a shell procedure, the shell | |
394 | will interpret the program in a subshell. The shell will | |
395 | reinitialize itself in this case, so that the effect will | |
396 | be as if a new shell had been invoked to handle the shell | |
dd00107c | 397 | procedure, except that the location of commands located in |
af143eb3 | 398 | the parent shell will be remembered by the child. |
dd00107c MT |
399 | .sp 2 |
400 | .B Path Search | |
401 | .sp | |
402 | .LP | |
af143eb3 KM |
403 | When locating a command, the shell first looks to see if |
404 | it has a shell function by that name. Then it looks for a | |
405 | builtin command by that name. | |
dd00107c MT |
406 | Finally, it searches each |
407 | entry in PATH in turn for the command. | |
408 | .LP | |
af143eb3 KM |
409 | The value of the PATH variable should be a series of |
410 | entries separated by colons. Each entry consists of a | |
7ab274c2 MT |
411 | directory name. |
412 | The current directory | |
413 | may be indicated by an empty directory name. | |
414 | .LP | |
415 | Command names containing a slash are simply executed without performing any of the above searches. | |
dd00107c MT |
416 | .sp 2 |
417 | .B Command Exit Status | |
418 | .sp | |
419 | .LP | |
420 | Each command has an exit status that can influence the behavior | |
421 | of other shell commands. The paradigm is that a command exits | |
422 | with zero for normal or success, and non-zero for failure, | |
423 | error, or a false indication. The man page for each command | |
51f8b609 | 424 | should indicate the various exit codes and what they mean. |
dd00107c MT |
425 | Additionally, the builtin commands return exit codes, as does |
426 | an executed function. | |
427 | .sp 2 | |
428 | .B Complex Commands | |
429 | .sp | |
430 | .LP | |
7ab274c2 MT |
431 | Complex commands are combinations of simple commands |
432 | with control operators or reserved words, together creating a larger complex | |
dd00107c MT |
433 | command. More generally, a command is one of the following: |
434 | .nf | |
435 | ||
436 | - simple command | |
af143eb3 | 437 | |
dd00107c MT |
438 | - pipeline |
439 | ||
440 | - list or compound-list | |
441 | ||
442 | - compound command | |
443 | ||
af143eb3 | 444 | - function definition |
dd00107c MT |
445 | |
446 | .fi | |
447 | .LP | |
448 | Unless otherwise stated, the exit status of a command is | |
449 | that of the last simple command executed by the command. | |
450 | .sp 2 | |
451 | .B Pipeline | |
452 | .sp | |
453 | .LP | |
454 | A pipeline is a sequence of one or more commands separated | |
455 | by the control operator |. The standard output of all but | |
456 | the last command is connected to the standard input | |
457 | of the next command. | |
458 | .LP | |
459 | The format for a pipeline is: | |
460 | .nf | |
461 | ||
462 | [!] command1 [ | command2 ...] | |
463 | ||
464 | .fi | |
465 | .LP | |
466 | The standard output of command1 is connected to the standard | |
467 | input of command2. The standard input, standard output, or | |
468 | both of a command is considered to be assigned by the | |
469 | pipeline before any redirection specified by redirection | |
470 | operators that are part of the command. | |
471 | .LP | |
472 | If the pipeline is not in the background (discussed later), | |
473 | the shell waits for all commands to complete. | |
474 | .LP | |
475 | If the reserved word ! does not precede the pipeline, the | |
476 | exit status is the exit status of the last command specified | |
477 | in the pipeline. Otherwise, the exit status is the logical | |
478 | NOT of the exit status of the last command. That is, if | |
479 | the last command returns zero, the exit status is 1; if | |
480 | the last command returns greater than zero, the exit status | |
65622797 | 481 | is zero. |
dd00107c MT |
482 | .LP |
483 | Because pipeline assignment of standard input or standard | |
484 | output or both takes place before redirection, it can be | |
485 | modified by redirection. For example: | |
486 | .nf | |
487 | ||
488 | $ command1 2>&1 | command2 | |
489 | ||
dcd7db01 | 490 | .fi |
dd00107c MT |
491 | sends both the standard output and standard error of command1 |
492 | to the standard input of command2. | |
493 | .LP | |
494 | A ; or <newline> terminator causes the preceding | |
7ab274c2 | 495 | AND-OR-list (described next) to be executed sequentially; a & causes |
dd00107c MT |
496 | asynchronous execution of the preceding AND-OR-list. |
497 | .sp 2 | |
498 | .B Background Commands -- & | |
499 | .sp | |
500 | .LP | |
501 | If a command is terminated by the control operator ampersand | |
502 | (&), the shell executes the command asynchronously -- that is, | |
503 | the shell does not wait for | |
504 | the command to finish before executing the next command. | |
505 | .LP | |
506 | The format for running a command in background is: | |
507 | .nf | |
508 | ||
509 | command1 & [command2 & ...] | |
510 | ||
511 | .fi | |
af143eb3 KM |
512 | If the shell is not interactive, the standard input of an |
513 | asynchronous command is set to /dev/null. | |
dd00107c MT |
514 | .sp 2 |
515 | .B Lists -- Generally Speaking | |
516 | .sp | |
517 | .LP | |
518 | A list is a sequence of zero or more commands separated by | |
af143eb3 KM |
519 | newlines, semicolons, or ampersands, |
520 | and optionally terminated by one of these three characters. | |
521 | The commands in a | |
522 | list are executed in the order they are written. | |
523 | If command is followed by an ampersand, the shell starts the | |
524 | command and immediately proceed onto the next command; | |
525 | otherwise it waits for the command to terminate before | |
dd00107c MT |
526 | proceeding to the next one. |
527 | .LP | |
af143eb3 KM |
528 | ``&&'' and ``||'' are AND-OR list operators. ``&&'' executes |
529 | the first command, and then executes the second command | |
530 | iff the exit status of the first command is zero. ``||'' | |
531 | is similar, but executes the second command iff the exit | |
dd00107c MT |
532 | status of the first command is nonzero. ``&&'' and ``||'' |
533 | both have the same priority. | |
534 | .LP | |
dcd7db01 | 535 | The syntax of the if command is |
dd00107c MT |
536 | .nf |
537 | ||
538 | if list | |
539 | then list | |
540 | [ elif list | |
541 | then list ] ... | |
542 | [ else list ] | |
543 | fi | |
544 | ||
545 | .fi | |
dcd7db01 | 546 | The syntax of the while command is |
dd00107c MT |
547 | .nf |
548 | ||
549 | while list | |
550 | do list | |
551 | done | |
552 | ||
553 | .fi | |
af143eb3 KM |
554 | The two lists are executed repeatedly while the exit status of the first list is zero. The until command is similar, but has the word until in place of while |
555 | repeat until the exit status of the first list is zero. | |
dd00107c | 556 | .LP |
dcd7db01 | 557 | The syntax of the for command is |
dd00107c | 558 | .nf |
dcd7db01 | 559 | |
dd00107c MT |
560 | for variable in word... |
561 | do list | |
562 | done | |
563 | ||
564 | .fi | |
af143eb3 | 565 | The words are expanded, and then the list is executed |
dd00107c MT |
566 | repeatedly with the variable set to each word in turn. do |
567 | and done may be replaced with ``{'' and ``}''. | |
568 | .LP | |
dcd7db01 | 569 | The syntax of the break and continue command is |
dd00107c MT |
570 | .nf |
571 | ||
572 | break [ num ] | |
573 | continue [ num ] | |
574 | ||
575 | .fi | |
af143eb3 KM |
576 | Break terminates the num innermost for or while loops. |
577 | Continue continues with the next iteration of the innermost loop. These are implemented as builtin commands. | |
dd00107c | 578 | .LP |
dcd7db01 | 579 | The syntax of the case command is |
dd00107c MT |
580 | .nf |
581 | ||
582 | case word in | |
583 | pattern) list ;; | |
584 | ... | |
585 | esac | |
586 | ||
587 | .fi | |
588 | .LP | |
af143eb3 | 589 | The pattern can actually be one or more patterns (see Shell |
dd00107c MT |
590 | Patterns described later), separated by ``|'' characters. |
591 | ||
592 | .LP | |
65622797 | 593 | Commands may be grouped by writing either |
dd00107c MT |
594 | .nf |
595 | ||
596 | (list) | |
597 | ||
598 | .fi | |
65622797 | 599 | or |
dd00107c MT |
600 | .nf |
601 | ||
602 | { list; } | |
603 | ||
604 | .fi | |
65622797 | 605 | The first of these executes the commands in a subshell. |
7ab274c2 MT |
606 | .sp 2 |
607 | .B Functions | |
608 | .sp | |
dd00107c | 609 | .LP |
dcd7db01 | 610 | The syntax of a function definition is |
65622797 | 611 | .nf |
dd00107c MT |
612 | |
613 | name ( ) command | |
614 | ||
615 | .fi | |
616 | .LP | |
af143eb3 | 617 | A function definition is an executable statement; when |
dd00107c | 618 | executed it installs a function named name and returns an |
af143eb3 | 619 | exit status of zero. The command is normally a list |
dd00107c MT |
620 | enclosed between ``{'' and ``}''. |
621 | .LP | |
af143eb3 KM |
622 | Variables may be declared to be local to a function by |
623 | using a local command. This should appear as the first | |
624 | statement of a function, and the syntax is | |
dd00107c MT |
625 | .nf |
626 | ||
627 | local [ variable | - ] ... | |
628 | ||
629 | .fi | |
630 | Local is implemented as a builtin command. | |
631 | .LP | |
af143eb3 KM |
632 | When a variable is made local, it inherits the initial |
633 | value and exported and readonly flags from the variable | |
634 | with the same name in the surrounding scope, if there is | |
635 | one. Otherwise, the variable is initially unset. The shell | |
636 | uses dynamic scoping, so that if you make the variable x | |
637 | local to function f, which then calls function g, references to the variable x made inside g will refer to the | |
638 | variable x declared inside f, not to the global variable | |
dd00107c MT |
639 | named x. |
640 | .LP | |
af143eb3 KM |
641 | The only special parameter than can be made local is |
642 | ``-''. Making ``-'' local any shell options that are | |
643 | changed via the set command inside the function to be | |
644 | restored to their original values when the function | |
dd00107c MT |
645 | returns. |
646 | .LP | |
dcd7db01 | 647 | The syntax of the return command is |
dd00107c MT |
648 | .nf |
649 | ||
650 | return [ exitstatus ] | |
651 | ||
652 | .fi | |
653 | It terminates the currently executing function. Return is | |
654 | implemented as a builtin command. | |
655 | .sp 2 | |
656 | .B Variables and Parameters | |
657 | .sp | |
658 | .LP | |
659 | The shell maintains a set of parameters. A parameter | |
660 | denoted by a name is called a variable. When starting up, | |
661 | the shell turns all the environment variables into shell | |
662 | variables. New variables can be set using the form | |
663 | .nf | |
664 | ||
665 | name=value | |
666 | ||
65622797 | 667 | .fi |
dd00107c MT |
668 | .LP |
669 | Variables set by the user must have a name consisting solely | |
670 | of alphabetics, numerics, and underscores - the first of which | |
671 | must not be numeric. A parameter can also be denoted by a number | |
672 | or a special character as explained below. | |
673 | .sp 2 | |
674 | .B Positional Parameters | |
65622797 | 675 | .sp |
dd00107c MT |
676 | .LP |
677 | A positional parameter is a parameter denoted by a number (n > 0). | |
678 | The shell sets these initially to the values of its command | |
af143eb3 | 679 | line arguments that follow the name of the shell script. |
dd00107c MT |
680 | The set(1) builtin can also be used to set or reset them. |
681 | .sp 2 | |
7ab274c2 | 682 | .B Special Parameters |
65622797 | 683 | .sp |
dd00107c MT |
684 | .LP |
685 | A special parameter is a parameter denoted by one of the following | |
686 | special characters. The value of the parameter is listed | |
687 | next to its character. | |
688 | .TP | |
689 | * | |
690 | Expands to the positional parameters, starting from one. When | |
691 | the expansion occurs within a double-quoted string | |
692 | it expands to a single field with the value of each parameter | |
693 | separated by the first character of the IFS variable, or by a | |
694 | <space> if IFS is unset. | |
695 | .TP | |
696 | @ | |
697 | Expands to the positional parameters, starting from one. When | |
698 | the expansion occurs within double-quotes, each positional | |
699 | parameter expands as a separate argument. | |
700 | If there are no positional parameters, the | |
7ab274c2 | 701 | expansion of @ generates zero arguments, even when @ is |
dd00107c | 702 | double-quoted. What this basically means, for example, is |
af143eb3 | 703 | if $1 is ``abc'' and $2 is ``def ghi'', then "$@" expands to |
dd00107c MT |
704 | the two arguments: |
705 | ||
706 | "abc" "def ghi" | |
707 | .TP | |
708 | # | |
709 | Expands to the number of positional parameters. | |
710 | .TP | |
711 | ? | |
712 | Expands to the exit status of the most recent pipeline. | |
713 | .TP | |
af143eb3 | 714 | - (Hyphen) |
dd00107c MT |
715 | Expands to the current option flags (the single-letter |
716 | option names concatenated into a string) as specified on | |
717 | invocation, by the set builtin command, or implicitly | |
718 | by the shell. | |
719 | .TP | |
720 | $ | |
721 | Expands to the process ID of the invoked shell. A subshell | |
722 | retains the same value of $ as its parent. | |
723 | .TP | |
724 | ! | |
725 | Expands to the process ID of the most recent background | |
726 | command executed from the current shell. For a | |
727 | pipeline, the process ID is that of the last command in the | |
728 | pipeline. | |
729 | .TP | |
af143eb3 KM |
730 | 0 (Zero.) |
731 | Expands to the name of the shell or shell script. | |
dd00107c MT |
732 | .LP |
733 | .sp 2 | |
734 | .B Word Expansions | |
65622797 | 735 | .sp |
dd00107c MT |
736 | .LP |
737 | This clause describes the various expansions that are | |
738 | performed on words. Not all expansions are performed on | |
739 | every word, as explained later. | |
740 | .LP | |
741 | Tilde expansions, parameter expansions, command substitutions, | |
742 | arithmetic expansions, and quote removals that occur within | |
743 | a single word expand to a single field. It is only field | |
744 | splitting or pathname expansion that can create multiple | |
745 | fields from a single word. The single exception to this | |
746 | rule is the expansion of the special parameter @ within | |
7ab274c2 | 747 | double-quotes, as was described above. |
dd00107c MT |
748 | .LP |
749 | The order of word expansion is: | |
750 | .LP | |
751 | (1) Tilde Expansion, Parameter Expansion, Command Substitution, | |
752 | Arithmetic Expansion (these all occur at the same time). | |
753 | .LP | |
754 | (2) Field Splitting is performed on fields | |
755 | generated by step (1) unless the IFS variable is null. | |
756 | .LP | |
757 | (3) Pathname Expansion (unless set -f is in effect). | |
758 | .LP | |
759 | (4) Quote Removal. | |
760 | .LP | |
761 | The $ character is used to introduce parameter expansion, command | |
af143eb3 | 762 | substitution, or arithmetic evaluation. |
dd00107c | 763 | .sp 2 |
af143eb3 | 764 | .B Tilde Expansion (substituting a user's home directory) |
dd00107c MT |
765 | .sp |
766 | .LP | |
767 | A word beginning with an unquoted tilde character (~) is | |
768 | subjected to tilde expansion. All the characters up to | |
af143eb3 KM |
769 | a slash (/) or the end of the word are treated as a username |
770 | and are replaced with the user's home directory. If the | |
7ab274c2 | 771 | username is missing (as in ~/foobar), the tilde is replaced |
af143eb3 | 772 | with the value of the HOME variable (the current user's |
dd00107c MT |
773 | home directory). |
774 | ||
775 | .sp 2 | |
776 | .B Parameter Expansion | |
65622797 | 777 | .sp |
dd00107c MT |
778 | .LP |
779 | The format for parameter expansion is as follows: | |
780 | .nf | |
781 | ||
782 | ${expression} | |
783 | ||
784 | .fi | |
785 | where expression consists of all characters until the matching }. Any } | |
786 | escaped by a backslash or within a quoted string, and characters in | |
787 | embedded arithmetic expansions, command substitutions, and variable | |
788 | expansions, are not examined in determining the matching }. | |
789 | .LP | |
790 | The simplest form for parameter expansion is: | |
791 | .nf | |
792 | ||
793 | ${parameter} | |
794 | ||
795 | .fi | |
796 | The value, if any, of parameter is substituted. | |
797 | .LP | |
798 | The parameter name or symbol can be enclosed in braces, which are | |
799 | optional except for positional parameters with more than one digit or | |
800 | when parameter is followed by a character that could be interpreted as | |
af143eb3 | 801 | part of the name. |
7ab274c2 | 802 | If a parameter expansion occurs inside |
dd00107c MT |
803 | double-quotes: |
804 | .LP | |
7ab274c2 | 805 | 1) Pathname expansion is not performed on the results of the |
dd00107c MT |
806 | expansion. |
807 | .LP | |
7ab274c2 | 808 | 2) Field splitting is not performed on the results of the |
dd00107c MT |
809 | expansion, with the exception of @. |
810 | .LP | |
811 | In addition, a parameter expansion can be modified by using one of the | |
af143eb3 | 812 | following formats. |
65622797 | 813 | .sp |
dd00107c | 814 | .TP |
af143eb3 | 815 | ${parameter:-word} |
dd00107c MT |
816 | Use Default Values. If parameter is unset or |
817 | null, the expansion of word is | |
818 | substituted; otherwise, the value of | |
819 | parameter is substituted. | |
820 | .TP | |
af143eb3 | 821 | ${parameter:=word} |
dd00107c MT |
822 | Assign Default Values. If parameter is unset |
823 | or null, the expansion of word is | |
824 | assigned to parameter. In all cases, the | |
825 | final value of parameter is | |
826 | substituted. Only variables, not positional | |
827 | parameters or special parameters, can be | |
828 | assigned in this way. | |
829 | .TP | |
830 | ${parameter:?[word]} | |
831 | Indicate Error if Null or Unset. If | |
832 | parameter is unset or null, the expansion of | |
833 | word (or a message indicating it is unset if | |
834 | word is omitted) is written to standard | |
835 | error and the shell exits with a nonzero | |
836 | exit status. Otherwise, the value of | |
837 | parameter is substituted. An | |
838 | interactive shell need not exit. | |
839 | .TP | |
840 | ${parameter:+word} | |
841 | Use Alternate Value. If parameter is unset | |
842 | or null, null is substituted; | |
843 | otherwise, the expansion of word is | |
844 | substituted. | |
845 | .LP | |
846 | In the parameter expansions shown previously, use of the colon in the | |
847 | format results in a test for a parameter that is unset or null; omission | |
848 | of the colon results in a test for a parameter that is only unset. | |
849 | .TP | |
850 | ${#parameter} | |
851 | String Length. The length in characters of | |
af143eb3 | 852 | the value of parameter. |
dd00107c MT |
853 | .LP |
854 | The following four varieties of parameter expansion provide for substring | |
855 | processing. In each case, pattern matching notation (see Shell Patterns), rather | |
856 | than regular expression notation, is used to evaluate the patterns. | |
857 | If parameter is * or @, the result of the expansion is unspecified. | |
858 | Enclosing the full parameter expansion string in double-quotes does not | |
859 | cause the following four varieties of pattern characters to be quoted, | |
860 | whereas quoting characters within the braces has this effect. | |
dd00107c MT |
861 | .TP |
862 | ${parameter%word} | |
863 | Remove Smallest Suffix Pattern. The word | |
864 | is expanded to produce a pattern. The | |
865 | parameter expansion then results in | |
866 | parameter, with the smallest portion of the | |
867 | suffix matched by the pattern deleted. | |
868 | ||
869 | .TP | |
870 | ${parameter%%word} | |
871 | Remove Largest Suffix Pattern. The word | |
872 | is expanded to produce a pattern. The | |
873 | parameter expansion then results in | |
874 | parameter, with the largest portion of the | |
875 | suffix matched by the pattern deleted. | |
876 | .TP | |
877 | ${parameter#word} | |
878 | Remove Smallest Prefix Pattern. The word | |
879 | is expanded to produce a pattern. The | |
880 | parameter expansion then results in | |
881 | parameter, with the smallest portion of the | |
882 | prefix matched by the pattern deleted. | |
883 | .TP | |
884 | ${parameter##word} | |
885 | Remove Largest Prefix Pattern. The word | |
886 | is expanded to produce a pattern. The | |
887 | parameter expansion then results in | |
888 | parameter, with the largest portion of the | |
889 | prefix matched by the pattern deleted. | |
890 | .LP | |
891 | .sp 2 | |
892 | .B Command Substitution | |
65622797 | 893 | .sp |
dd00107c MT |
894 | .LP |
895 | Command substitution allows the output of a command to be substituted in | |
896 | place of the command name itself. Command substitution occurs when | |
897 | the command is enclosed as follows: | |
898 | .nf | |
899 | ||
900 | $(command) | |
901 | ||
902 | .fi | |
903 | or (``backquoted'' version): | |
904 | .nf | |
905 | ||
906 | `command` | |
907 | ||
908 | .fi | |
909 | .LP | |
910 | The shell expands the command substitution by executing command in a | |
911 | subshell environment and replacing the command substitution | |
7ab274c2 | 912 | with the |
dd00107c MT |
913 | standard output of the command, removing sequences of one or more |
914 | <newline>s at the end of the substitution. (Embedded <newline>s before | |
915 | the end of the output are not removed; however, during field | |
916 | splitting, they may be translated into <space>s, depending on the value | |
917 | of IFS and quoting that is in effect.) | |
918 | ||
919 | .sp 2 | |
920 | .B Arithmetic Expansion | |
65622797 | 921 | .sp |
dd00107c MT |
922 | .LP |
923 | Arithmetic expansion provides a mechanism for evaluating an arithmetic | |
924 | expression and substituting its value. The format for arithmetic | |
925 | expansion is as follows: | |
926 | .nf | |
927 | ||
928 | $((expression)) | |
929 | ||
930 | .fi | |
931 | The expression is treated as if it were in double-quotes, except | |
932 | that a double-quote inside the expression is not treated specially. The | |
933 | shell expands all tokens in the expression for parameter expansion, | |
934 | command substitution, and quote removal. | |
935 | .LP | |
936 | Next, the shell treats this as an arithmetic expression and | |
af143eb3 | 937 | substitutes the value of the expression. |
dd00107c MT |
938 | |
939 | .sp 2 | |
940 | .B White Space Splitting (Field Splitting) | |
65622797 | 941 | .sp |
dd00107c MT |
942 | .LP |
943 | After parameter expansion, command substitution, and | |
944 | arithmetic expansion the shell scans the results of | |
945 | expansions and substitutions that did not occur in double-quotes for | |
946 | field splitting and multiple fields can result. | |
947 | .LP | |
948 | The shell treats each character of the IFS as a delimiter and use | |
949 | the delimiters to split the results of parameter expansion and command | |
950 | substitution into fields. | |
951 | ||
952 | .sp 2 | |
953 | .B Pathname Expansion (File Name Generation) | |
65622797 | 954 | .sp |
dd00107c | 955 | .LP |
af143eb3 | 956 | Unless the -f flag is set, file name generation is performed after word splitting is complete. Each word is |
dd00107c | 957 | viewed as a series of patterns, separated by slashes. The |
af143eb3 KM |
958 | process of expansion replaces the word with the names of |
959 | all existing files whose names can be formed by replacing | |
960 | each pattern with a string that matches the specified pattern. | |
961 | There are two restrictions on this: first, a pattern cannot match a string containing a slash, and second, | |
962 | a pattern cannot match a string starting with a period | |
dd00107c MT |
963 | unless the first character of the pattern is a period. |
964 | The next section describes the patterns used for both | |
965 | Pathname Expansion and the case(1) command. | |
966 | ||
967 | .sp 2 | |
968 | .B Shell Patterns | |
65622797 | 969 | .sp |
dd00107c | 970 | .LP |
af143eb3 KM |
971 | A pattern consists of normal characters, which match themselves, and meta-characters. The meta-characters are |
972 | ``!'', ``*'', ``?'', and ``[''. These characters lose | |
973 | there special meanings if they are quoted. When command | |
974 | or variable substitution is performed and the dollar sign | |
975 | or back quotes are not double quoted, the value of the | |
dd00107c MT |
976 | variable or the output of the command is scanned for these |
977 | characters and they are turned into meta-characters. | |
978 | .LP | |
af143eb3 KM |
979 | An asterisk (``*'') matches any string of characters. A |
980 | question mark matches any single character. A left | |
981 | bracket (``['') introduces a character class. The end of | |
982 | the character class is indicated by a ``]''; if the ``]'' | |
983 | is missing then the ``['' matches a ``['' rather than | |
984 | introducing a character class. A character class matches | |
985 | any of the characters between the square brackets. A | |
986 | range of characters may be specified using a minus sign. | |
987 | The character class may be complemented by making an | |
988 | exclamation point the first character of the character | |
dd00107c MT |
989 | class. |
990 | .LP | |
991 | To include a ``]'' in a character class, make it the first | |
af143eb3 | 992 | character listed (after the ``!'', if any). To include a |
dd00107c MT |
993 | minus sign, make it the first or last character listed |
994 | ||
995 | .sp 2 | |
996 | .B Builtins | |
65622797 | 997 | .sp |
dd00107c | 998 | .LP |
af143eb3 KM |
999 | This section lists the builtin commands which |
1000 | are builtin because they need to perform some operation | |
1001 | that can't be performed by a separate process. In addition to these, there are several other commands that may | |
dd00107c MT |
1002 | be builtin for efficiency (e.g. printf(1), echo(1), test(1), |
1003 | etc). | |
1004 | .TP | |
34a1a05a CZ |
1005 | : |
1006 | A null command that returns a 0 (true) exit value. | |
1007 | .TP | |
1008 | \&. file | |
1009 | The commands in the specified file are read and executed by the shell. | |
1010 | .TP | |
dd00107c MT |
1011 | alias [ name[=string] ... ] |
1012 | If name=string is specified, the shell defines the | |
1013 | alias ``name'' with value ``string''. If just ``name'' | |
1014 | is specified, the value of the alias ``name'' is printed. | |
1015 | With no arguments, the alias builtin prints the | |
1016 | names and values of all defined aliases (see unalias). | |
1017 | .TP | |
1018 | bg [ job ] ... | |
1019 | Continue the specified jobs (or the current job if no | |
af143eb3 | 1020 | jobs are given) in the background. |
dd00107c MT |
1021 | .TP |
1022 | command command arg... | |
7ab274c2 | 1023 | Execute the specified builtin command. (This is useful when you have a shell function with the same name |
dd00107c MT |
1024 | as a builtin command.) |
1025 | .TP | |
1026 | cd [ directory ] | |
af143eb3 | 1027 | Switch to the specified directory (default $HOME). |
dd00107c MT |
1028 | If the an entry for CDPATH appears in the environment |
1029 | of the cd command or the shell variable CDPATH is set | |
af143eb3 KM |
1030 | and the directory name does not begin with a slash, |
1031 | then the directories listed in CDPATH will be | |
1032 | searched for the specified directory. The format of | |
1033 | CDPATH is the same as that of PATH. In an interactive shell, the cd command will print out the name of | |
dd00107c | 1034 | the directory that it actually switched to if this is |
af143eb3 KM |
1035 | different from the name that the user gave. These |
1036 | may be different either because the CDPATH mechanism | |
dd00107c MT |
1037 | was used or because a symbolic link was crossed. |
1038 | .TP | |
dd00107c MT |
1039 | eval string... |
1040 | Concatenate all the arguments with spaces. Then | |
1041 | re-parse and execute the command. | |
1042 | .TP | |
1043 | exec [ command arg... ] | |
af143eb3 KM |
1044 | Unless command is omitted, the shell process is |
1045 | replaced with the specified program (which must be a | |
dd00107c | 1046 | real program, not a shell builtin or function). Any |
af143eb3 | 1047 | redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes. |
dd00107c MT |
1048 | .TP |
1049 | exit [ exitstatus ] | |
af143eb3 | 1050 | Terminate the shell process. If exitstatus is given |
dd00107c MT |
1051 | it is used as the exit status of the shell; otherwise |
1052 | the exit status of the preceding command is used. | |
1053 | .TP | |
1054 | export name... | |
af143eb3 KM |
1055 | The specified names are exported so that they will |
1056 | appear in the environment of subsequent commands. | |
1057 | The only way to un-export a variable is to unset it. | |
1058 | The shell allows the value of a variable to be set at the | |
dd00107c MT |
1059 | same time it is exported by writing |
1060 | .nf | |
1061 | ||
1062 | export name=value | |
1063 | ||
1064 | .fi | |
af143eb3 | 1065 | With no arguments the export command lists the names |
dd00107c MT |
1066 | of all exported variables. |
1067 | .TP | |
1068 | fc [-e editor] [first [last]] | |
1069 | .TP | |
1070 | fc -l [-nr] [first [last]] | |
1071 | .TP | |
1072 | fc -s [old=new] [first] | |
1073 | The fc builtin lists, or edits and re-executes, commands | |
1074 | previously entered to an interactive shell. | |
1075 | .RS +.5i | |
1076 | .TP 2 | |
af143eb3 | 1077 | -e editor |
dd00107c MT |
1078 | Use the editor named by editor to edit the commands. The |
1079 | editor string is a command name, subject to search via the | |
1080 | PATH variable. The value in the FCEDIT variable | |
1081 | is used as a default when -e is not specified. If | |
1082 | FCEDIT is null or unset, the value of the EDITOR | |
1083 | variable is used. If EDITOR is null or unset, | |
1084 | ed(1) is used as the editor. | |
1085 | .TP 2 | |
1086 | -l (ell) | |
1087 | List the commands rather than invoking | |
1088 | an editor on them. The commands are written in the | |
1089 | sequence indicated by the first and last operands, as | |
1090 | affected by -r, with each command preceded by the command | |
1091 | number. | |
1092 | .TP 2 | |
1093 | -n | |
1094 | Suppress command numbers when listing with -l. | |
1095 | .TP 2 | |
1096 | -r | |
1097 | Reverse the order of the commands listed (with -l) or | |
1098 | edited (with neither -l nor -s). | |
1099 | .TP 2 | |
af143eb3 | 1100 | -s |
dd00107c MT |
1101 | Re-execute the command without invoking an editor. |
1102 | .TP 2 | |
1103 | first | |
1104 | .TP 2 | |
af143eb3 | 1105 | last |
dd00107c MT |
1106 | Select the commands to list or edit. The number of |
1107 | previous commands that can be accessed are determined | |
1108 | by the value of the HISTSIZE variable. The value of first | |
1109 | or last or both are one of the following: | |
1110 | .TP 2 | |
af143eb3 | 1111 | [+]number |
dd00107c MT |
1112 | A positive number representing a command |
1113 | number; command numbers can be displayed | |
1114 | with the -l option. | |
1115 | .TP 2 | |
af143eb3 | 1116 | -number |
dd00107c MT |
1117 | A negative decimal number representing the |
1118 | command that was executed number of | |
1119 | commands previously. For example, -1 is | |
1120 | the immediately previous command. | |
1121 | .TP 2 | |
af143eb3 | 1122 | string |
dd00107c MT |
1123 | A string indicating the most recently |
1124 | entered command that begins with that | |
1125 | string. If the old=new operand is not also | |
1126 | specified with -s, the string form of the | |
1127 | first operand cannot contain an embedded | |
1128 | equal sign. | |
1129 | .TP | |
1130 | The following environment variables affect the execution of fc: | |
1131 | .TP 2 | |
af143eb3 | 1132 | FCEDIT |
dd00107c MT |
1133 | Name of the editor to use. |
1134 | .TP 2 | |
af143eb3 KM |
1135 | HISTSIZE |
1136 | The number of previous commands that are accessable. | |
dd00107c MT |
1137 | .RE |
1138 | .TP | |
1139 | fg [ job ] | |
af143eb3 KM |
1140 | Move the specified job or the current job to the |
1141 | foreground. | |
dd00107c MT |
1142 | .TP |
1143 | getopts optstring var | |
1144 | The POSIX getopts command. | |
1145 | .TP | |
1146 | hash -rv command... | |
1147 | The shell maintains a hash table which remembers the | |
1148 | locations of commands. With no arguments whatsoever, | |
af143eb3 KM |
1149 | the hash command prints out the contents of this |
1150 | table. Entries which have not been looked at since | |
1151 | the last cd command are marked with an asterisk; it | |
dd00107c | 1152 | is possible for these entries to be invalid. |
65622797 | 1153 | .sp |
af143eb3 KM |
1154 | With arguments, the hash command removes the specified commands from the hash table (unless they are |
1155 | functions) and then locates them. With the -v | |
1156 | option, hash prints the locations of the commands as | |
dd00107c | 1157 | it finds them. The -r option causes the hash command |
af143eb3 | 1158 | to delete all the entries in the hash table except |
dd00107c MT |
1159 | for functions. |
1160 | .TP | |
1161 | jobid [ job ] | |
af143eb3 KM |
1162 | Print the process id's of the processes in the job. |
1163 | If the job argument is omitted, use the current job. | |
dd00107c MT |
1164 | .TP |
1165 | jobs | |
af143eb3 | 1166 | This command lists out all the background processes |
dd00107c MT |
1167 | which are children of the current shell process. |
1168 | .TP | |
1169 | pwd | |
1170 | Print the current directory. The builtin command may | |
af143eb3 KM |
1171 | differ from the program of the same name because the |
1172 | builtin command remembers what the current directory | |
1173 | is rather than recomputing it each time. This makes | |
1174 | it faster. However, if the current directory is | |
1175 | renamed, the builtin version of pwd will continue to | |
dd00107c MT |
1176 | print the old name for the directory. |
1177 | .TP | |
1178 | read [ -p prompt ] [ -e ] variable... | |
af143eb3 | 1179 | The prompt is printed if the -p option is specified |
dd00107c | 1180 | and the standard input is a terminal. Then a line is |
af143eb3 KM |
1181 | read from the standard input. The trailing newline |
1182 | is deleted from the line and the line is split as | |
dd00107c | 1183 | described in the section on word splitting above, and |
af143eb3 KM |
1184 | the pieces are assigned to the variables in order. |
1185 | If there are more pieces than variables, the remaining pieces (along with the characters in IFS that | |
1186 | separated them) are assigned to the last variable. | |
1187 | If there are more variables than pieces, the remaining variables are assigned the null string. | |
65622797 | 1188 | .sp |
af143eb3 KM |
1189 | The -e option causes any backslashes in the input to |
1190 | be treated specially. If a backslash is followed by | |
1191 | a newline, the backslash and the newline will be | |
1192 | deleted. If a backslash is followed by any other | |
1193 | character, the backslash will be deleted and the following character will be treated as though it were | |
dd00107c MT |
1194 | not in IFS, even if it is. |
1195 | .TP | |
1196 | readonly name... | |
af143eb3 KM |
1197 | The specified names are marked as read only, so that |
1198 | they cannot be subsequently modified or unset. The shell | |
1199 | allows the value of a variable to be set at the same | |
dd00107c MT |
1200 | time it is marked read only by writing |
1201 | .TP | |
1202 | readonly name=value | |
af143eb3 | 1203 | With no arguments the readonly command lists the |
dd00107c MT |
1204 | names of all read only variables. |
1205 | .TP | |
1206 | set [ { -options | +options | -- } ] arg... | |
1207 | The set command performs three different functions. | |
65622797 | 1208 | .sp |
af143eb3 | 1209 | With no arguments, it lists the values of all shell |
dd00107c | 1210 | variables. |
65622797 | 1211 | .sp |
af143eb3 | 1212 | If options are given, it sets the specified option |
dd00107c MT |
1213 | flags, or clears them as described in the section |
1214 | called ``Argument List Processing''. | |
1215 | .sp | |
1216 | The third use of the set command is to set the values | |
1217 | of the shell's positional parameters to the specified | |
af143eb3 KM |
1218 | args. To change the positional parameters without |
1219 | changing any options, use ``--'' as the first argument to set. If no args are present, the set command | |
dd00107c MT |
1220 | will clear all the positional parameters (equivalent |
1221 | to executing ``shift $#''. | |
1222 | .TP | |
1223 | setvar variable value | |
af143eb3 KM |
1224 | Assigns value to variable. (In general it is better |
1225 | to write variable=value rather than using setvar. | |
1226 | Setvar is intended to be used in functions that | |
1227 | assign values to variables whose names are passed as | |
dd00107c MT |
1228 | parameters.) |
1229 | .TP | |
1230 | shift [ n ] | |
af143eb3 | 1231 | Shift the positional parameters n times. A shift |
dd00107c | 1232 | sets the value of $1 to the value of $2, the value of |
af143eb3 KM |
1233 | $2 to the value of $3, and so on, decreasing the |
1234 | value of $# by one. If there are zero positional | |
dd00107c MT |
1235 | parameters, shifting doesn't do anything. |
1236 | .TP | |
1237 | trap [ action ] signal... | |
af143eb3 KM |
1238 | Cause the shell to parse and execute action when any |
1239 | of the specified signals are received. The signals | |
1240 | are specified by signal number. Action may be null | |
dd00107c | 1241 | or omitted; the former causes the specified signal to |
af143eb3 KM |
1242 | be ignored and the latter causes the default action |
1243 | to be taken. When the shell forks off a subshell, it | |
1244 | resets trapped (but not ignored) signals to the | |
1245 | default action. The trap command has no effect on | |
dd00107c MT |
1246 | signals that were ignored on entry to the shell. |
1247 | .TP | |
1248 | umask [ mask ] | |
af143eb3 KM |
1249 | Set the value of umask (see umask(2)) to the specified octal value. |
1250 | If the argument is omitted, the | |
dd00107c MT |
1251 | umask value is printed. |
1252 | .TP | |
1253 | unalias [-a] [name] | |
1254 | If ``name'' is specified, the shell removes that alias. | |
1255 | If ``-a'' is specified, all aliases are removed. | |
1256 | .TP | |
1257 | unset name... | |
af143eb3 KM |
1258 | The specified variables and functions are unset and |
1259 | unexported. If a given name corresponds to both a | |
1260 | variable and a function, both the variable and the | |
dd00107c MT |
1261 | function are unset. |
1262 | .TP | |
1263 | wait [ job ] | |
1264 | Wait for the specified job to complete and return the | |
af143eb3 KM |
1265 | exit status of the last process in the job. If the |
1266 | argument is omitted, wait for all jobs to complete | |
dd00107c | 1267 | and the return an exit status of zero. |
7ab274c2 | 1268 | .LP |
dd00107c MT |
1269 | .sp 2 |
1270 | .B Command Line Editing | |
1271 | .sp | |
1272 | .LP | |
1273 | When sh is being used interactively from a terminal, the current command | |
1274 | and the command history (see fc in Builtins) can be edited using vi-mode | |
af143eb3 | 1275 | command-line editing. This mode uses commands, described below, similar |
dd00107c MT |
1276 | to a subset of those described in the vi man page. |
1277 | The command set -o vi enables vi-mode editing and place sh into vi | |
1278 | insert mode. | |
1279 | With vi-mode enabled, sh can be switched between insert mode and command | |
af143eb3 KM |
1280 | mode. The editor is not described in full here, but will be in a later |
1281 | document. It's similar to vi: typing <ESC> will throw you into | |
1282 | command VI command mode. Hitting <return> while in command mode | |
dd00107c | 1283 | will pass the line to the shell. |