The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | .\" $NetBSD: sh.1,v 1.78 2004/06/03 19:54:37 hubertf Exp $ |
| 2 | .\" Copyright (c) 1991, 1993 |
| 3 | .\" The Regents of the University of California. All rights reserved. |
| 4 | .\" |
| 5 | .\" This code is derived from software contributed to Berkeley by |
| 6 | .\" Kenneth Almquist. |
| 7 | .\" |
| 8 | .\" Redistribution and use in source and binary forms, with or without |
| 9 | .\" modification, are permitted provided that the following conditions |
| 10 | .\" are met: |
| 11 | .\" 1. Redistributions of source code must retain the above copyright |
| 12 | .\" notice, this list of conditions and the following disclaimer. |
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 14 | .\" notice, this list of conditions and the following disclaimer in the |
| 15 | .\" documentation and/or other materials provided with the distribution. |
| 16 | .\" 3. Neither the name of the University nor the names of its contributors |
| 17 | .\" may be used to endorse or promote products derived from this software |
| 18 | .\" without specific prior written permission. |
| 19 | .\" |
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. |
| 31 | .\" |
| 32 | .\" @(#)sh.1 8.6 (Berkeley) 5/4/95 |
| 33 | .\" |
| 34 | .Dd April 17, 2004 |
| 35 | .Os |
| 36 | .Dt SH 1 |
| 37 | .Sh NAME |
| 38 | .Nm sh |
| 39 | .Nd command interpreter (shell) |
| 40 | .Sh SYNOPSIS |
| 41 | .Nm |
| 42 | .Bk -words |
| 43 | .Op Fl aCefnuvxIimqVEb |
| 44 | .Op Cm +aCefnuvxIimqVEb |
| 45 | .Ek |
| 46 | .Bk -words |
| 47 | .Op Fl o Ar option_name |
| 48 | .Op Cm +o Ar option_name |
| 49 | .Ek |
| 50 | .Bk -words |
| 51 | .Op Ar command_file Oo Ar argument ... Oc |
| 52 | .Ek |
| 53 | .Nm |
| 54 | .Fl c |
| 55 | .Bk -words |
| 56 | .Op Fl aCefnuvxIimqVEb |
| 57 | .Op Cm +aCefnuvxIimqVEb |
| 58 | .Ek |
| 59 | .Bk -words |
| 60 | .Op Fl o Ar option_name |
| 61 | .Op Cm +o Ar option_name |
| 62 | .Ek |
| 63 | .Bk -words |
| 64 | .Ar command_string |
| 65 | .Op Ar command_name Oo Ar argument ... Oc |
| 66 | .Ek |
| 67 | .Nm |
| 68 | .Fl s |
| 69 | .Bk -words |
| 70 | .Op Fl aCefnuvxIimqVEb |
| 71 | .Op Cm +aCefnuvxIimqVEb |
| 72 | .Ek |
| 73 | .Bk -words |
| 74 | .Op Fl o Ar option_name |
| 75 | .Op Cm +o Ar option_name |
| 76 | .Ek |
| 77 | .Bk -words |
| 78 | .Op Ar argument ... |
| 79 | .Ek |
| 80 | .Sh DESCRIPTION |
| 81 | .Nm |
| 82 | is the standard command interpreter for the system. |
| 83 | The current version of |
| 84 | .Nm |
| 85 | is in the process of being changed to conform with the |
| 86 | .Tn POSIX |
| 87 | 1003.2 and 1003.2a specifications for the shell. |
| 88 | This version has many |
| 89 | features which make it appear similar in some respects to the Korn shell, |
| 90 | but it is not a Korn shell clone (see |
| 91 | .Xr ksh 1 ) . |
| 92 | Only features designated by |
| 93 | .Tn POSIX , |
| 94 | plus a few Berkeley extensions, are being incorporated into this shell. |
| 95 | .\" We expect |
| 96 | .\" .Tn POSIX |
| 97 | .\" conformance by the time 4.4 BSD is released. |
| 98 | This man page is not intended |
| 99 | to be a tutorial or a complete specification of the shell. |
| 100 | .Ss Overview |
| 101 | The shell is a command that reads lines from either a file or the |
| 102 | terminal, interprets them, and generally executes other commands. |
| 103 | It is the program that is running when a user logs into the system |
| 104 | (although a user can select a different shell with the |
| 105 | .Xr chsh 1 |
| 106 | command). |
| 107 | The shell implements a language that has flow control |
| 108 | constructs, a macro facility that provides a variety of features in |
| 109 | addition to data storage, along with built in history and line editing |
| 110 | capabilities. |
| 111 | It incorporates many features to aid interactive use and |
| 112 | has the advantage that the interpretative language is common to both |
| 113 | interactive and non-interactive use (shell scripts). |
| 114 | That is, commands |
| 115 | can be typed directly to the running shell or can be put into a file and |
| 116 | the file can be executed directly by the shell. |
| 117 | .Ss Invocation |
| 118 | If no args are present and if the standard input of the shell |
| 119 | is connected to a terminal (or if the |
| 120 | .Fl i |
| 121 | flag is set), |
| 122 | and the |
| 123 | .Fl c |
| 124 | option is not present, the shell is considered an interactive shell. |
| 125 | An interactive shell generally prompts before each command and handles |
| 126 | programming and command errors differently (as described below). |
| 127 | When first starting, |
| 128 | the shell inspects argument 0, and if it begins with a dash |
| 129 | .Sq - , |
| 130 | the shell is also considered |
| 131 | a login shell. |
| 132 | This is normally done automatically by the system |
| 133 | when the user first logs in. |
| 134 | A login shell first reads commands |
| 135 | from the files |
| 136 | .Pa /etc/profile |
| 137 | and |
| 138 | .Pa .profile |
| 139 | if they exist. |
| 140 | If the environment variable |
| 141 | .Ev ENV |
| 142 | is set on entry to a shell, or is set in the |
| 143 | .Pa .profile |
| 144 | of a login shell, the shell next reads |
| 145 | commands from the file named in |
| 146 | .Ev ENV . |
| 147 | Therefore, a user should place commands that are to be executed only at |
| 148 | login time in the |
| 149 | .Pa .profile |
| 150 | file, and commands that are executed for every shell inside the |
| 151 | .Ev ENV |
| 152 | file. |
| 153 | To set the |
| 154 | .Ev ENV |
| 155 | variable to some file, place the following line in your |
| 156 | .Pa .profile |
| 157 | of your home directory |
| 158 | .Pp |
| 159 | .Dl ENV=$HOME/.shinit; export ENV |
| 160 | .Pp |
| 161 | substituting for |
| 162 | .Dq .shinit |
| 163 | any filename you wish. |
| 164 | Since the |
| 165 | .Ev ENV |
| 166 | file is read for every invocation of the shell, including shell scripts |
| 167 | and non-interactive shells, the following paradigm is useful for |
| 168 | restricting commands in the |
| 169 | .Ev ENV |
| 170 | file to interactive invocations. |
| 171 | Place commands within the |
| 172 | .Dq case |
| 173 | and |
| 174 | .Dq esac |
| 175 | below (these commands are described later): |
| 176 | .Pp |
| 177 | .Bl -item -compact -offset indent |
| 178 | .It |
| 179 | .Li case $- in *i*) |
| 180 | .Bl -item -compact -offset indent |
| 181 | .It |
| 182 | .Li # commands for interactive use only |
| 183 | .It |
| 184 | .Li ... |
| 185 | .El |
| 186 | .It |
| 187 | .Li esac |
| 188 | .El |
| 189 | .Pp |
| 190 | If command line arguments besides the options have been specified, then |
| 191 | the shell treats the first argument as the name of a file from which to |
| 192 | read commands (a shell script), and the remaining arguments are set as the |
| 193 | positional parameters of the shell ($1, $2, etc). |
| 194 | Otherwise, the shell |
| 195 | reads commands from its standard input. |
| 196 | .Ss Argument List Processing |
| 197 | All of the single letter options have a corresponding name that can be |
| 198 | used as an argument to the |
| 199 | .Fl o |
| 200 | option. |
| 201 | The set |
| 202 | .Fl o |
| 203 | name is provided next to the single letter option in |
| 204 | the description below. |
| 205 | Specifying a dash |
| 206 | .Dq - |
| 207 | turns the option on, while using a plus |
| 208 | .Dq + |
| 209 | disables the option. |
| 210 | The following options can be set from the command line or |
| 211 | with the |
| 212 | .Ic set |
| 213 | builtin (described later). |
| 214 | .Bl -tag -width aaaallexportfoo -offset indent |
| 215 | .It Fl a Em allexport |
| 216 | Export all variables assigned to. |
| 217 | .It Fl c |
| 218 | Read commands from the |
| 219 | .Ar command_string |
| 220 | operand instead of from the standard input. |
| 221 | Special parameter 0 will be set from the |
| 222 | .Ar command_name |
| 223 | operand and the positional parameters ($1, $2, etc.) |
| 224 | set from the remaining argument operands. |
| 225 | .It Fl C Em noclobber |
| 226 | Don't overwrite existing files with |
| 227 | .Dq \*[Gt] . |
| 228 | .It Fl e Em errexit |
| 229 | If not interactive, exit immediately if any untested command fails. |
| 230 | The exit status of a command is considered to be |
| 231 | explicitly tested if the command is used to control an |
| 232 | .Ic if , |
| 233 | .Ic elif , |
| 234 | .Ic while , |
| 235 | or |
| 236 | .Ic until ; |
| 237 | or if the command is the left hand operand of an |
| 238 | .Dq \*[Am]\*[Am] |
| 239 | or |
| 240 | .Dq || |
| 241 | operator. |
| 242 | .It Fl f Em noglob |
| 243 | Disable pathname expansion. |
| 244 | .It Fl n Em noexec |
| 245 | If not interactive, read commands but do not execute them. |
| 246 | This is useful for checking the syntax of shell scripts. |
| 247 | .It Fl u Em nounset |
| 248 | Write a message to standard error when attempting to expand a variable |
| 249 | that is not set, and if the shell is not interactive, exit immediately. |
| 250 | .It Fl v Em verbose |
| 251 | The shell writes its input to standard error as it is read. |
| 252 | Useful for debugging. |
| 253 | .It Fl x Em xtrace |
| 254 | Write each command to standard error (preceded by a |
| 255 | .Sq +\ ) |
| 256 | before it is executed. |
| 257 | Useful for debugging. |
| 258 | .It Fl q Em quietprofile |
| 259 | If the |
| 260 | .Fl v |
| 261 | or |
| 262 | .Fl x |
| 263 | options have been set, do not apply them when reading |
| 264 | initialization files, these being |
| 265 | .Pa /etc/profile , |
| 266 | .Pa .profile , |
| 267 | and the file specified by the |
| 268 | .Ev ENV |
| 269 | environment variable. |
| 270 | .It Fl I Em ignoreeof |
| 271 | Ignore EOF's from input when interactive. |
| 272 | .It Fl i Em interactive |
| 273 | Force the shell to behave interactively. |
| 274 | .It Fl m Em monitor |
| 275 | Turn on job control (set automatically when interactive). |
| 276 | .It Fl s Em stdin |
| 277 | Read commands from standard input (set automatically if no file arguments |
| 278 | are present). |
| 279 | This option has no effect when set after the shell has |
| 280 | already started running (i.e. with |
| 281 | .Ic set ) . |
| 282 | .It Fl V Em vi |
| 283 | Enable the built-in |
| 284 | .Xr vi 1 |
| 285 | command line editor (disables |
| 286 | .Fl E |
| 287 | if it has been set). |
| 288 | (See the |
| 289 | .Sx Command Line Editing |
| 290 | section below.) |
| 291 | .It Fl E Em emacs |
| 292 | Enable the built-in emacs style |
| 293 | command line editor (disables |
| 294 | .Fl V |
| 295 | if it has been set). |
| 296 | (See the |
| 297 | .Sx Command Line Editing |
| 298 | section below.) |
| 299 | .It Fl b Em notify |
| 300 | Enable asynchronous notification of background job completion. |
| 301 | (UNIMPLEMENTED for 4.4alpha) |
| 302 | .It "\ \ " Em cdprint |
| 303 | Make an interactive shell always print the new directory name when |
| 304 | changed by the |
| 305 | .Ic cd |
| 306 | command. |
| 307 | .El |
| 308 | .Ss Lexical Structure |
| 309 | The shell reads input in terms of lines from a file and breaks it up into |
| 310 | words at whitespace (blanks and tabs), and at certain sequences of |
| 311 | characters that are special to the shell called |
| 312 | .Dq operators . |
| 313 | There are two types of operators: control operators and redirection |
| 314 | operators (their meaning is discussed later). |
| 315 | Following is a list of operators: |
| 316 | .Bl -ohang -offset indent |
| 317 | .It "Control operators:" |
| 318 | .Dl \*[Am] \*[Am]\*[Am] \&( \&) \&; ;; | || \*[Lt]newline\*[Gt] |
| 319 | .It "Redirection operators:" |
| 320 | .Dl \*[Lt] \*[Gt] \*[Gt]| \*[Lt]\*[Lt] \*[Gt]\*[Gt] \*[Lt]\*[Am] \*[Gt]\*[Am] \*[Lt]\*[Lt]- \*[Lt]\*[Gt] |
| 321 | .El |
| 322 | .Ss Quoting |
| 323 | Quoting is used to remove the special meaning of certain characters or |
| 324 | words to the shell, such as operators, whitespace, or keywords. |
| 325 | There are three types of quoting: matched single quotes, |
| 326 | matched double quotes, and backslash. |
| 327 | .Ss Backslash |
| 328 | A backslash preserves the literal meaning of the following |
| 329 | character, with the exception of |
| 330 | .Aq newline . |
| 331 | A backslash preceding a |
| 332 | .Aq newline |
| 333 | is treated as a line continuation. |
| 334 | .Ss Single Quotes |
| 335 | Enclosing characters in single quotes preserves the literal meaning of all |
| 336 | the characters (except single quotes, making it impossible to put |
| 337 | single-quotes in a single-quoted string). |
| 338 | .Ss Double Quotes |
| 339 | Enclosing characters within double quotes preserves the literal |
| 340 | meaning of all characters except dollarsign |
| 341 | .Pq $ , |
| 342 | backquote |
| 343 | .Pq ` , |
| 344 | and backslash |
| 345 | .Pq \e . |
| 346 | The backslash inside double quotes is historically weird, and serves to |
| 347 | quote only the following characters: |
| 348 | .Dl $ ` \*q \e \*[Lt]newline\*[Gt] . |
| 349 | Otherwise it remains literal. |
| 350 | .Ss Reserved Words |
| 351 | Reserved words are words that have special meaning to the |
| 352 | shell and are recognized at the beginning of a line and |
| 353 | after a control operator. |
| 354 | The following are reserved words: |
| 355 | .Bl -column while while while while while -offset indent |
| 356 | .It ! Ta elif Ta fi Ta while Ta case |
| 357 | .It else Ta for Ta then Ta { Ta } |
| 358 | .It do Ta done Ta until Ta if Ta esac |
| 359 | .El |
| 360 | .Pp |
| 361 | Their meaning is discussed later. |
| 362 | .Ss Aliases |
| 363 | An alias is a name and corresponding value set using the |
| 364 | .Ic alias |
| 365 | builtin command. |
| 366 | Whenever a reserved word may occur (see above), |
| 367 | and after checking for reserved words, the shell |
| 368 | checks the word to see if it matches an alias. |
| 369 | If it does, it replaces it in the input stream with its value. |
| 370 | For example, if there is an alias called |
| 371 | .Dq lf |
| 372 | with the value |
| 373 | .Dq "ls -F" , |
| 374 | then the input: |
| 375 | .Pp |
| 376 | .Dl lf foobar Aq return |
| 377 | .Pp |
| 378 | would become |
| 379 | .Pp |
| 380 | .Dl ls -F foobar Aq return |
| 381 | .Pp |
| 382 | Aliases provide a convenient way for naive users to create shorthands for |
| 383 | commands without having to learn how to create functions with arguments. |
| 384 | They can also be used to create lexically obscure code. |
| 385 | This use is discouraged. |
| 386 | .Ss Commands |
| 387 | The shell interprets the words it reads according to a language, the |
| 388 | specification of which is outside the scope of this man page (refer to the |
| 389 | BNF in the |
| 390 | .Tn POSIX |
| 391 | 1003.2 document). |
| 392 | Essentially though, a line is read and if the first |
| 393 | word of the line (or after a control operator) is not a reserved word, |
| 394 | then the shell has recognized a simple command. |
| 395 | Otherwise, a complex |
| 396 | command or some other special construct may have been recognized. |
| 397 | .Ss Simple Commands |
| 398 | If a simple command has been recognized, the shell performs |
| 399 | the following actions: |
| 400 | .Bl -enum -offset indent |
| 401 | .It |
| 402 | Leading words of the form |
| 403 | .Dq name=value |
| 404 | are stripped off and assigned to the environment of the simple command. |
| 405 | Redirection operators and their arguments (as described below) are |
| 406 | stripped off and saved for processing. |
| 407 | .It |
| 408 | The remaining words are expanded as described in |
| 409 | the section called |
| 410 | .Dq Expansions , |
| 411 | and the first remaining word is considered the command name and the |
| 412 | command is located. |
| 413 | The remaining words are considered the arguments of the command. |
| 414 | If no command name resulted, then the |
| 415 | .Dq name=value |
| 416 | variable assignments recognized in item 1 affect the current shell. |
| 417 | .It |
| 418 | Redirections are performed as described in the next section. |
| 419 | .El |
| 420 | .Ss Redirections |
| 421 | Redirections are used to change where a command reads its input or sends |
| 422 | its output. |
| 423 | In general, redirections open, close, or duplicate an |
| 424 | existing reference to a file. |
| 425 | The overall format used for redirection is: |
| 426 | .Pp |
| 427 | .Dl [n] Va redir-op Ar file |
| 428 | .Pp |
| 429 | where |
| 430 | .Va redir-op |
| 431 | is one of the redirection operators mentioned previously. |
| 432 | Following is a list of the possible redirections. |
| 433 | The |
| 434 | .Bq n |
| 435 | is an optional number, as in |
| 436 | .Sq 3 |
| 437 | (not |
| 438 | .Sq Bq 3 ) , |
| 439 | that refers to a file descriptor. |
| 440 | .Bl -tag -width aaabsfiles -offset indent |
| 441 | .It [n] Ns \*[Gt] file |
| 442 | Redirect standard output (or n) to file. |
| 443 | .It [n] Ns \*[Gt]| file |
| 444 | Same, but override the |
| 445 | .Fl C |
| 446 | option. |
| 447 | .It [n] Ns \*[Gt]\*[Gt] file |
| 448 | Append standard output (or n) to file. |
| 449 | .It [n] Ns \*[Lt] file |
| 450 | Redirect standard input (or n) from file. |
| 451 | .It [n1] Ns \*[Lt]\*[Am] Ns n2 |
| 452 | Duplicate standard input (or n1) from file descriptor n2. |
| 453 | .It [n] Ns \*[Lt]\*[Am]- |
| 454 | Close standard input (or n). |
| 455 | .It [n1] Ns \*[Gt]\*[Am] Ns n2 |
| 456 | Duplicate standard output (or n1) to n2. |
| 457 | .It [n] Ns \*[Gt]\*[Am]- |
| 458 | Close standard output (or n). |
| 459 | .It [n] Ns \*[Lt]\*[Gt] file |
| 460 | Open file for reading and writing on standard input (or n). |
| 461 | .El |
| 462 | .Pp |
| 463 | The following redirection is often called a |
| 464 | .Dq here-document . |
| 465 | .Bl -item -offset indent |
| 466 | .It |
| 467 | .Li [n]\*[Lt]\*[Lt] delimiter |
| 468 | .Dl here-doc-text ... |
| 469 | .Li delimiter |
| 470 | .El |
| 471 | .Pp |
| 472 | All the text on successive lines up to the delimiter is saved away and |
| 473 | made available to the command on standard input, or file descriptor n if |
| 474 | it is specified. |
| 475 | If the delimiter as specified on the initial line is |
| 476 | quoted, then the here-doc-text is treated literally, otherwise the text is |
| 477 | subjected to parameter expansion, command substitution, and arithmetic |
| 478 | expansion (as described in the section on |
| 479 | .Dq Expansions ) . |
| 480 | If the operator is |
| 481 | .Dq \*[Lt]\*[Lt]- |
| 482 | instead of |
| 483 | .Dq \*[Lt]\*[Lt] , |
| 484 | then leading tabs in the here-doc-text are stripped. |
| 485 | .Ss Search and Execution |
| 486 | There are three types of commands: shell functions, builtin commands, and |
| 487 | normal programs -- and the command is searched for (by name) in that order. |
| 488 | They each are executed in a different way. |
| 489 | .Pp |
| 490 | When a shell function is executed, all of the shell positional parameters |
| 491 | (except $0, which remains unchanged) are set to the arguments of the shell |
| 492 | function. |
| 493 | The variables which are explicitly placed in the environment of |
| 494 | the command (by placing assignments to them before the function name) are |
| 495 | made local to the function and are set to the values given. |
| 496 | Then the command given in the function definition is executed. |
| 497 | The positional parameters are restored to their original values |
| 498 | when the command completes. |
| 499 | This all occurs within the current shell. |
| 500 | .Pp |
| 501 | Shell builtins are executed internally to the shell, without spawning a |
| 502 | new process. |
| 503 | .Pp |
| 504 | Otherwise, if the command name doesn't match a function or builtin, the |
| 505 | command is searched for as a normal program in the file system (as |
| 506 | described in the next section). |
| 507 | When a normal program is executed, the shell runs the program, |
| 508 | passing the arguments and the environment to the program. |
| 509 | If the program is not a normal executable file (i.e., if it does |
| 510 | not begin with the "magic number" whose |
| 511 | .Tn ASCII |
| 512 | representation is "#!", so |
| 513 | .Xr execve 2 |
| 514 | returns |
| 515 | .Er ENOEXEC |
| 516 | then) the shell will interpret the program in a subshell. |
| 517 | The child shell will reinitialize itself in this case, |
| 518 | so that the effect will be as if a |
| 519 | new shell had been invoked to handle the ad-hoc shell script, except that |
| 520 | the location of hashed commands located in the parent shell will be |
| 521 | remembered by the child. |
| 522 | .Pp |
| 523 | Note that previous versions of this document and the source code itself |
| 524 | misleadingly and sporadically refer to a shell script without a magic |
| 525 | number as a "shell procedure". |
| 526 | .Ss Path Search |
| 527 | When locating a command, the shell first looks to see if it has a shell |
| 528 | function by that name. |
| 529 | Then it looks for a builtin command by that name. |
| 530 | If a builtin command is not found, one of two things happen: |
| 531 | .Bl -enum |
| 532 | .It |
| 533 | Command names containing a slash are simply executed without performing |
| 534 | any searches. |
| 535 | .It |
| 536 | The shell searches each entry in |
| 537 | .Ev PATH |
| 538 | in turn for the command. |
| 539 | The value of the |
| 540 | .Ev PATH |
| 541 | variable should be a series of entries separated by colons. |
| 542 | Each entry consists of a directory name. |
| 543 | The current directory may be indicated |
| 544 | implicitly by an empty directory name, or explicitly by a single period. |
| 545 | .El |
| 546 | .Ss Command Exit Status |
| 547 | Each command has an exit status that can influence the behavior |
| 548 | of other shell commands. |
| 549 | The paradigm is that a command exits |
| 550 | with zero for normal or success, and non-zero for failure, |
| 551 | error, or a false indication. |
| 552 | The man page for each command |
| 553 | should indicate the various exit codes and what they mean. |
| 554 | Additionally, the builtin commands return exit codes, as does |
| 555 | an executed shell function. |
| 556 | .Pp |
| 557 | If a command consists entirely of variable assignments then the |
| 558 | exit status of the command is that of the last command substitution |
| 559 | if any, otherwise 0. |
| 560 | .Ss Complex Commands |
| 561 | Complex commands are combinations of simple commands with control |
| 562 | operators or reserved words, together creating a larger complex command. |
| 563 | More generally, a command is one of the following: |
| 564 | .Bl -bullet |
| 565 | .It |
| 566 | simple command |
| 567 | .It |
| 568 | pipeline |
| 569 | .It |
| 570 | list or compound-list |
| 571 | .It |
| 572 | compound command |
| 573 | .It |
| 574 | function definition |
| 575 | .El |
| 576 | .Pp |
| 577 | Unless otherwise stated, the exit status of a command is that of the last |
| 578 | simple command executed by the command. |
| 579 | .Ss Pipelines |
| 580 | A pipeline is a sequence of one or more commands separated |
| 581 | by the control operator |. |
| 582 | The standard output of all but |
| 583 | the last command is connected to the standard input |
| 584 | of the next command. |
| 585 | The standard output of the last |
| 586 | command is inherited from the shell, as usual. |
| 587 | .Pp |
| 588 | The format for a pipeline is: |
| 589 | .Pp |
| 590 | .Dl [!] command1 [ | command2 ...] |
| 591 | .Pp |
| 592 | The standard output of command1 is connected to the standard input of |
| 593 | command2. |
| 594 | The standard input, standard output, or both of a command is |
| 595 | considered to be assigned by the pipeline before any redirection specified |
| 596 | by redirection operators that are part of the command. |
| 597 | .Pp |
| 598 | If the pipeline is not in the background (discussed later), the shell |
| 599 | waits for all commands to complete. |
| 600 | .Pp |
| 601 | If the reserved word ! does not precede the pipeline, the exit status is |
| 602 | the exit status of the last command specified in the pipeline. |
| 603 | Otherwise, the exit status is the logical NOT of the exit status of the |
| 604 | last command. |
| 605 | That is, if the last command returns zero, the exit status |
| 606 | is 1; if the last command returns greater than zero, the exit status is |
| 607 | zero. |
| 608 | .Pp |
| 609 | Because pipeline assignment of standard input or standard output or both |
| 610 | takes place before redirection, it can be modified by redirection. |
| 611 | For example: |
| 612 | .Pp |
| 613 | .Dl $ command1 2\*[Gt]\*[Am]1 | command2 |
| 614 | .Pp |
| 615 | sends both the standard output and standard error of command1 |
| 616 | to the standard input of command2. |
| 617 | .Pp |
| 618 | A ; or |
| 619 | .Aq newline |
| 620 | terminator causes the preceding AND-OR-list (described |
| 621 | next) to be executed sequentially; a \*[Am] causes asynchronous execution of |
| 622 | the preceding AND-OR-list. |
| 623 | .Pp |
| 624 | Note that unlike some other shells, each process in the pipeline is a |
| 625 | child of the invoking shell (unless it is a shell builtin, in which case |
| 626 | it executes in the current shell -- but any effect it has on the |
| 627 | environment is wiped). |
| 628 | .Ss Background Commands -- \*[Am] |
| 629 | If a command is terminated by the control operator ampersand (\*[Am]), the |
| 630 | shell executes the command asynchronously -- that is, the shell does not |
| 631 | wait for the command to finish before executing the next command. |
| 632 | .Pp |
| 633 | The format for running a command in background is: |
| 634 | .Pp |
| 635 | .Dl command1 \*[Am] [command2 \*[Am] ...] |
| 636 | .Pp |
| 637 | If the shell is not interactive, the standard input of an asynchronous |
| 638 | command is set to |
| 639 | .Pa /dev/null . |
| 640 | .Ss Lists -- Generally Speaking |
| 641 | A list is a sequence of zero or more commands separated by newlines, |
| 642 | semicolons, or ampersands, and optionally terminated by one of these three |
| 643 | characters. |
| 644 | The commands in a list are executed in the order they are written. |
| 645 | If command is followed by an ampersand, the shell starts the |
| 646 | command and immediately proceed onto the next command; otherwise it waits |
| 647 | for the command to terminate before proceeding to the next one. |
| 648 | .Ss Short-Circuit List Operators |
| 649 | .Dq \*[Am]\*[Am] |
| 650 | and |
| 651 | .Dq || |
| 652 | are AND-OR list operators. |
| 653 | .Dq \*[Am]\*[Am] |
| 654 | executes the first command, and then executes the second command if and only |
| 655 | if the exit status of the first command is zero. |
| 656 | .Dq || |
| 657 | is similar, but executes the second command if and only if the exit status |
| 658 | of the first command is nonzero. |
| 659 | .Dq \*[Am]\*[Am] |
| 660 | and |
| 661 | .Dq || |
| 662 | both have the same priority. |
| 663 | Note that these operators are left-associative, so |
| 664 | .Dq true || echo bar && echo baz |
| 665 | writes |
| 666 | .Dq baz |
| 667 | and nothing else. |
| 668 | This is not the way it works in C. |
| 669 | .Ss Flow-Control Constructs -- if, while, for, case |
| 670 | The syntax of the if command is |
| 671 | .Bd -literal -offset indent |
| 672 | if list |
| 673 | then list |
| 674 | [ elif list |
| 675 | then list ] ... |
| 676 | [ else list ] |
| 677 | fi |
| 678 | .Ed |
| 679 | .Pp |
| 680 | The syntax of the while command is |
| 681 | .Bd -literal -offset indent |
| 682 | while list |
| 683 | do list |
| 684 | done |
| 685 | .Ed |
| 686 | .Pp |
| 687 | The two lists are executed repeatedly while the exit status of the |
| 688 | first list is zero. |
| 689 | The until command is similar, but has the word |
| 690 | until in place of while, which causes it to |
| 691 | repeat until the exit status of the first list is zero. |
| 692 | .Pp |
| 693 | The syntax of the for command is |
| 694 | .Bd -literal -offset indent |
| 695 | for variable in word ... |
| 696 | do list |
| 697 | done |
| 698 | .Ed |
| 699 | .Pp |
| 700 | The words are expanded, and then the list is executed repeatedly with the |
| 701 | variable set to each word in turn. |
| 702 | do and done may be replaced with |
| 703 | .Dq { |
| 704 | and |
| 705 | .Dq } . |
| 706 | .Pp |
| 707 | The syntax of the break and continue command is |
| 708 | .Bd -literal -offset indent |
| 709 | break [ num ] |
| 710 | continue [ num ] |
| 711 | .Ed |
| 712 | .Pp |
| 713 | Break terminates the num innermost for or while loops. |
| 714 | Continue continues with the next iteration of the innermost loop. |
| 715 | These are implemented as builtin commands. |
| 716 | .Pp |
| 717 | The syntax of the case command is |
| 718 | .Bd -literal -offset indent |
| 719 | case word in |
| 720 | pattern) list ;; |
| 721 | \&... |
| 722 | esac |
| 723 | .Ed |
| 724 | .Pp |
| 725 | The pattern can actually be one or more patterns (see |
| 726 | .Sx Shell Patterns |
| 727 | described later), separated by |
| 728 | .Dq \*(Ba |
| 729 | characters. |
| 730 | .Ss Grouping Commands Together |
| 731 | Commands may be grouped by writing either |
| 732 | .Pp |
| 733 | .Dl (list) |
| 734 | .Pp |
| 735 | or |
| 736 | .Pp |
| 737 | .Dl { list; } |
| 738 | .Pp |
| 739 | The first of these executes the commands in a subshell. |
| 740 | Builtin commands grouped into a (list) will not affect the current shell. |
| 741 | The second form does not fork another shell so is slightly more efficient. |
| 742 | Grouping commands together this way allows you to redirect |
| 743 | their output as though they were one program: |
| 744 | .Pp |
| 745 | .Bd -literal -offset indent |
| 746 | { echo -n \*q hello \*q ; echo \*q world" ; } \*[Gt] greeting |
| 747 | .Ed |
| 748 | .Pp |
| 749 | Note that |
| 750 | .Dq } |
| 751 | must follow a control operator (here, |
| 752 | .Dq \&; ) |
| 753 | so that it is recognized as a reserved word and not as another command argument. |
| 754 | .Ss Functions |
| 755 | The syntax of a function definition is |
| 756 | .Pp |
| 757 | .Dl name ( ) command |
| 758 | .Pp |
| 759 | A function definition is an executable statement; when executed it |
| 760 | installs a function named name and returns an exit status of zero. |
| 761 | The command is normally a list enclosed between |
| 762 | .Dq { |
| 763 | and |
| 764 | .Dq } . |
| 765 | .Pp |
| 766 | Variables may be declared to be local to a function by using a local |
| 767 | command. |
| 768 | This should appear as the first statement of a function, and the syntax is |
| 769 | .Pp |
| 770 | .Dl local [ variable | - ] ... |
| 771 | .Pp |
| 772 | Local is implemented as a builtin command. |
| 773 | .Pp |
| 774 | When a variable is made local, it inherits the initial value and exported |
| 775 | and readonly flags from the variable with the same name in the surrounding |
| 776 | scope, if there is one. |
| 777 | Otherwise, the variable is initially unset. |
| 778 | The shell uses dynamic scoping, so that if you make the variable x local to |
| 779 | function f, which then calls function g, references to the variable x made |
| 780 | inside g will refer to the variable x declared inside f, not to the global |
| 781 | variable named x. |
| 782 | .Pp |
| 783 | The only special parameter that can be made local is |
| 784 | .Dq - . |
| 785 | Making |
| 786 | .Dq - |
| 787 | local any shell options that are changed via the set command inside the |
| 788 | function to be restored to their original values when the function |
| 789 | returns. |
| 790 | .Pp |
| 791 | The syntax of the return command is |
| 792 | .Pp |
| 793 | .Dl return [ exitstatus ] |
| 794 | .Pp |
| 795 | It terminates the currently executing function. |
| 796 | Return is implemented as a builtin command. |
| 797 | .Ss Variables and Parameters |
| 798 | The shell maintains a set of parameters. |
| 799 | A parameter denoted by a name is called a variable. |
| 800 | When starting up, the shell turns all the environment |
| 801 | variables into shell variables. |
| 802 | New variables can be set using the form |
| 803 | .Pp |
| 804 | .Dl name=value |
| 805 | .Pp |
| 806 | Variables set by the user must have a name consisting solely of |
| 807 | alphabetics, numerics, and underscores - the first of which must not be |
| 808 | numeric. |
| 809 | A parameter can also be denoted by a number or a special |
| 810 | character as explained below. |
| 811 | .Ss Positional Parameters |
| 812 | A positional parameter is a parameter denoted by a number (n \*[Gt] 0). |
| 813 | The shell sets these initially to the values of its command line arguments |
| 814 | that follow the name of the shell script. |
| 815 | The |
| 816 | .Ic set |
| 817 | builtin can also be used to set or reset them. |
| 818 | .Ss Special Parameters |
| 819 | A special parameter is a parameter denoted by one of the following special |
| 820 | characters. |
| 821 | The value of the parameter is listed next to its character. |
| 822 | .Bl -tag -width thinhyphena |
| 823 | .It * |
| 824 | Expands to the positional parameters, starting from one. |
| 825 | When the |
| 826 | expansion occurs within a double-quoted string it expands to a single |
| 827 | field with the value of each parameter separated by the first character of |
| 828 | the |
| 829 | .Ev IFS |
| 830 | variable, or by a |
| 831 | .Aq space |
| 832 | if |
| 833 | .Ev IFS |
| 834 | is unset. |
| 835 | .It @ |
| 836 | Expands to the positional parameters, starting from one. |
| 837 | When the expansion occurs within double-quotes, each positional |
| 838 | parameter expands as a separate argument. |
| 839 | If there are no positional parameters, the |
| 840 | expansion of @ generates zero arguments, even when @ is |
| 841 | double-quoted. |
| 842 | What this basically means, for example, is |
| 843 | if $1 is |
| 844 | .Dq abc |
| 845 | and $2 is |
| 846 | .Dq def ghi , |
| 847 | then |
| 848 | .Qq $@ |
| 849 | expands to |
| 850 | the two arguments: |
| 851 | .Pp |
| 852 | .Sm off |
| 853 | .Dl \*q abc \*q \ \*q def\ ghi \*q |
| 854 | .Sm on |
| 855 | .It # |
| 856 | Expands to the number of positional parameters. |
| 857 | .It \&? |
| 858 | Expands to the exit status of the most recent pipeline. |
| 859 | .It - (Hyphen.) |
| 860 | Expands to the current option flags (the single-letter |
| 861 | option names concatenated into a string) as specified on |
| 862 | invocation, by the set builtin command, or implicitly |
| 863 | by the shell. |
| 864 | .It $ |
| 865 | Expands to the process ID of the invoked shell. |
| 866 | A subshell retains the same value of $ as its parent. |
| 867 | .It \&! |
| 868 | Expands to the process ID of the most recent background |
| 869 | command executed from the current shell. |
| 870 | For a pipeline, the process ID is that of the last command in the pipeline. |
| 871 | .It 0 (Zero.) |
| 872 | Expands to the name of the shell or shell script. |
| 873 | .El |
| 874 | .Ss Word Expansions |
| 875 | This clause describes the various expansions that are performed on words. |
| 876 | Not all expansions are performed on every word, as explained later. |
| 877 | .Pp |
| 878 | Tilde expansions, parameter expansions, command substitutions, arithmetic |
| 879 | expansions, and quote removals that occur within a single word expand to a |
| 880 | single field. |
| 881 | It is only field splitting or pathname expansion that can |
| 882 | create multiple fields from a single word. |
| 883 | The single exception to this |
| 884 | rule is the expansion of the special parameter @ within double-quotes, as |
| 885 | was described above. |
| 886 | .Pp |
| 887 | The order of word expansion is: |
| 888 | .Bl -enum |
| 889 | .It |
| 890 | Tilde Expansion, Parameter Expansion, Command Substitution, |
| 891 | Arithmetic Expansion (these all occur at the same time). |
| 892 | .It |
| 893 | Field Splitting is performed on fields |
| 894 | generated by step (1) unless the |
| 895 | .Ev IFS |
| 896 | variable is null. |
| 897 | .It |
| 898 | Pathname Expansion (unless set |
| 899 | .Fl f |
| 900 | is in effect). |
| 901 | .It |
| 902 | Quote Removal. |
| 903 | .El |
| 904 | .Pp |
| 905 | The $ character is used to introduce parameter expansion, command |
| 906 | substitution, or arithmetic evaluation. |
| 907 | .Ss Tilde Expansion (substituting a user's home directory) |
| 908 | A word beginning with an unquoted tilde character (~) is |
| 909 | subjected to tilde expansion. |
| 910 | All the characters up to |
| 911 | a slash (/) or the end of the word are treated as a username |
| 912 | and are replaced with the user's home directory. |
| 913 | If the username is missing (as in |
| 914 | .Pa ~/foobar ) , |
| 915 | the tilde is replaced with the value of the |
| 916 | .Va HOME |
| 917 | variable (the current user's home directory). |
| 918 | .Ss Parameter Expansion |
| 919 | The format for parameter expansion is as follows: |
| 920 | .Pp |
| 921 | .Dl ${expression} |
| 922 | .Pp |
| 923 | where expression consists of all characters until the matching |
| 924 | .Dq } . |
| 925 | Any |
| 926 | .Dq } |
| 927 | escaped by a backslash or within a quoted string, and characters in |
| 928 | embedded arithmetic expansions, command substitutions, and variable |
| 929 | expansions, are not examined in determining the matching |
| 930 | .Dq } . |
| 931 | .Pp |
| 932 | The simplest form for parameter expansion is: |
| 933 | .Pp |
| 934 | .Dl ${parameter} |
| 935 | .Pp |
| 936 | The value, if any, of parameter is substituted. |
| 937 | .Pp |
| 938 | The parameter name or symbol can be enclosed in braces, which are |
| 939 | optional except for positional parameters with more than one digit or |
| 940 | when parameter is followed by a character that could be interpreted as |
| 941 | part of the name. |
| 942 | If a parameter expansion occurs inside double-quotes: |
| 943 | .Bl -enum |
| 944 | .It |
| 945 | Pathname expansion is not performed on the results of the expansion. |
| 946 | .It |
| 947 | Field splitting is not performed on the results of the |
| 948 | expansion, with the exception of @. |
| 949 | .El |
| 950 | .Pp |
| 951 | In addition, a parameter expansion can be modified by using one of the |
| 952 | following formats. |
| 953 | .Bl -tag -width aaparameterwordaaaaa |
| 954 | .It ${parameter:-word} |
| 955 | Use Default Values. |
| 956 | If parameter is unset or null, the expansion of word |
| 957 | is substituted; otherwise, the value of parameter is substituted. |
| 958 | .It ${parameter:=word} |
| 959 | Assign Default Values. |
| 960 | If parameter is unset or null, the expansion of |
| 961 | word is assigned to parameter. |
| 962 | In all cases, the final value of parameter is substituted. |
| 963 | Only variables, not positional parameters or special |
| 964 | parameters, can be assigned in this way. |
| 965 | .It ${parameter:?[word]} |
| 966 | Indicate Error if Null or Unset. |
| 967 | If parameter is unset or null, the |
| 968 | expansion of word (or a message indicating it is unset if word is omitted) |
| 969 | is written to standard error and the shell exits with a nonzero exit status. |
| 970 | Otherwise, the value of parameter is substituted. |
| 971 | An interactive shell need not exit. |
| 972 | .It ${parameter:+word} |
| 973 | Use Alternative Value. |
| 974 | If parameter is unset or null, null is |
| 975 | substituted; otherwise, the expansion of word is substituted. |
| 976 | .El |
| 977 | .Pp |
| 978 | In the parameter expansions shown previously, use of the colon in the |
| 979 | format results in a test for a parameter that is unset or null; omission |
| 980 | of the colon results in a test for a parameter that is only unset. |
| 981 | .Bl -tag -width aaparameterwordaaaaa |
| 982 | .It ${#parameter} |
| 983 | String Length. |
| 984 | The length in characters of the value of parameter. |
| 985 | .El |
| 986 | .Pp |
| 987 | The following four varieties of parameter expansion provide for substring |
| 988 | processing. |
| 989 | In each case, pattern matching notation (see |
| 990 | .Sx Shell Patterns ) , |
| 991 | rather than regular expression notation, is used to evaluate the patterns. |
| 992 | If parameter is * or @, the result of the expansion is unspecified. |
| 993 | Enclosing the full parameter expansion string in double-quotes does not |
| 994 | cause the following four varieties of pattern characters to be quoted, |
| 995 | whereas quoting characters within the braces has this effect. |
| 996 | .Bl -tag -width aaparameterwordaaaaa |
| 997 | .It ${parameter%word} |
| 998 | Remove Smallest Suffix Pattern. |
| 999 | The word is expanded to produce a pattern. |
| 1000 | The parameter expansion then results in parameter, with the |
| 1001 | smallest portion of the suffix matched by the pattern deleted. |
| 1002 | .It ${parameter%%word} |
| 1003 | Remove Largest Suffix Pattern. |
| 1004 | The word is expanded to produce a pattern. |
| 1005 | The parameter expansion then results in parameter, with the largest |
| 1006 | portion of the suffix matched by the pattern deleted. |
| 1007 | .It ${parameter#word} |
| 1008 | Remove Smallest Prefix Pattern. |
| 1009 | The word is expanded to produce a pattern. |
| 1010 | The parameter expansion then results in parameter, with the |
| 1011 | smallest portion of the prefix matched by the pattern deleted. |
| 1012 | .It ${parameter##word} |
| 1013 | Remove Largest Prefix Pattern. |
| 1014 | The word is expanded to produce a pattern. |
| 1015 | The parameter expansion then results in parameter, with the largest |
| 1016 | portion of the prefix matched by the pattern deleted. |
| 1017 | .El |
| 1018 | .Ss Command Substitution |
| 1019 | Command substitution allows the output of a command to be substituted in |
| 1020 | place of the command name itself. |
| 1021 | Command substitution occurs when the command is enclosed as follows: |
| 1022 | .Pp |
| 1023 | .Dl $(command) |
| 1024 | .Pp |
| 1025 | or |
| 1026 | .Po |
| 1027 | .Dq backquoted |
| 1028 | version |
| 1029 | .Pc : |
| 1030 | .Pp |
| 1031 | .Dl `command` |
| 1032 | .Pp |
| 1033 | The shell expands the command substitution by executing command in a |
| 1034 | subshell environment and replacing the command substitution with the |
| 1035 | standard output of the command, removing sequences of one or more |
| 1036 | .Ao newline Ac Ns s |
| 1037 | at the end of the substitution. |
| 1038 | (Embedded |
| 1039 | .Ao newline Ac Ns s |
| 1040 | before |
| 1041 | the end of the output are not removed; however, during field splitting, |
| 1042 | they may be translated into |
| 1043 | .Ao space Ac Ns s , |
| 1044 | depending on the value of |
| 1045 | .Ev IFS |
| 1046 | and quoting that is in effect.) |
| 1047 | .Ss Arithmetic Expansion |
| 1048 | Arithmetic expansion provides a mechanism for evaluating an arithmetic |
| 1049 | expression and substituting its value. |
| 1050 | The format for arithmetic expansion is as follows: |
| 1051 | .Pp |
| 1052 | .Dl $((expression)) |
| 1053 | .Pp |
| 1054 | The expression is treated as if it were in double-quotes, except |
| 1055 | that a double-quote inside the expression is not treated specially. |
| 1056 | The shell expands all tokens in the expression for parameter expansion, |
| 1057 | command substitution, and quote removal. |
| 1058 | .Pp |
| 1059 | Next, the shell treats this as an arithmetic expression and |
| 1060 | substitutes the value of the expression. |
| 1061 | .Ss White Space Splitting (Field Splitting) |
| 1062 | After parameter expansion, command substitution, and |
| 1063 | arithmetic expansion the shell scans the results of |
| 1064 | expansions and substitutions that did not occur in double-quotes for |
| 1065 | field splitting and multiple fields can result. |
| 1066 | .Pp |
| 1067 | The shell treats each character of the |
| 1068 | .Ev IFS |
| 1069 | as a delimiter and use the delimiters to split the results of parameter |
| 1070 | expansion and command substitution into fields. |
| 1071 | .Ss Pathname Expansion (File Name Generation) |
| 1072 | Unless the |
| 1073 | .Fl f |
| 1074 | flag is set, file name generation is performed after word splitting is |
| 1075 | complete. |
| 1076 | Each word is viewed as a series of patterns, separated by slashes. |
| 1077 | The process of expansion replaces the word with the names of all |
| 1078 | existing files whose names can be formed by replacing each pattern with a |
| 1079 | string that matches the specified pattern. |
| 1080 | There are two restrictions on |
| 1081 | this: first, a pattern cannot match a string containing a slash, and |
| 1082 | second, a pattern cannot match a string starting with a period unless the |
| 1083 | first character of the pattern is a period. |
| 1084 | The next section describes the |
| 1085 | patterns used for both Pathname Expansion and the |
| 1086 | .Ic case |
| 1087 | command. |
| 1088 | .Ss Shell Patterns |
| 1089 | A pattern consists of normal characters, which match themselves, |
| 1090 | and meta-characters. |
| 1091 | The meta-characters are |
| 1092 | .Dq \&! , |
| 1093 | .Dq * , |
| 1094 | .Dq \&? , |
| 1095 | and |
| 1096 | .Dq \&[ . |
| 1097 | These characters lose their special meanings if they are quoted. |
| 1098 | When command or variable substitution is performed |
| 1099 | and the dollar sign or back quotes are not double quoted, |
| 1100 | the value of the variable or the output of |
| 1101 | the command is scanned for these characters and they are turned into |
| 1102 | meta-characters. |
| 1103 | .Pp |
| 1104 | An asterisk |
| 1105 | .Pq Dq * |
| 1106 | matches any string of characters. |
| 1107 | A question mark matches any single character. |
| 1108 | A left bracket |
| 1109 | .Pq Dq \&[ |
| 1110 | introduces a character class. |
| 1111 | The end of the character class is indicated by a |
| 1112 | .Pq Dq \&] ; |
| 1113 | if the |
| 1114 | .Dq \&] |
| 1115 | is missing then the |
| 1116 | .Dq \&[ |
| 1117 | matches a |
| 1118 | .Dq \&[ |
| 1119 | rather than introducing a character class. |
| 1120 | A character class matches any of the characters between the square brackets. |
| 1121 | A range of characters may be specified using a minus sign. |
| 1122 | The character class may be complemented |
| 1123 | by making an exclamation point the first character of the character class. |
| 1124 | .Pp |
| 1125 | To include a |
| 1126 | .Dq \&] |
| 1127 | in a character class, make it the first character listed (after the |
| 1128 | .Dq \&! , |
| 1129 | if any). |
| 1130 | To include a minus sign, make it the first or last character listed. |
| 1131 | .Ss Builtins |
| 1132 | This section lists the builtin commands which are builtin because they |
| 1133 | need to perform some operation that can't be performed by a separate |
| 1134 | process. |
| 1135 | In addition to these, there are several other commands that may |
| 1136 | be builtin for efficiency (e.g. |
| 1137 | .Xr printf 1 , |
| 1138 | .Xr echo 1 , |
| 1139 | .Xr test 1 , |
| 1140 | etc). |
| 1141 | .Bl -tag -width 5n |
| 1142 | .It : |
| 1143 | A null command that returns a 0 (true) exit value. |
| 1144 | .It \&. file |
| 1145 | The commands in the specified file are read and executed by the shell. |
| 1146 | .It alias Op Ar name Ns Op Ar "=string ..." |
| 1147 | If |
| 1148 | .Ar name=string |
| 1149 | is specified, the shell defines the alias |
| 1150 | .Ar name |
| 1151 | with value |
| 1152 | .Ar string . |
| 1153 | If just |
| 1154 | .Ar name |
| 1155 | is specified, the value of the alias |
| 1156 | .Ar name |
| 1157 | is printed. |
| 1158 | With no arguments, the |
| 1159 | .Ic alias |
| 1160 | builtin prints the |
| 1161 | names and values of all defined aliases (see |
| 1162 | .Ic unalias ) . |
| 1163 | .It bg [ Ar job ] ... |
| 1164 | Continue the specified jobs (or the current job if no |
| 1165 | jobs are given) in the background. |
| 1166 | .It Xo command |
| 1167 | .Op Fl p |
| 1168 | .Op Fl v |
| 1169 | .Op Fl V |
| 1170 | .Ar command |
| 1171 | .Op Ar arg ... |
| 1172 | .Xc |
| 1173 | Execute the specified command but ignore shell functions when searching |
| 1174 | for it. |
| 1175 | (This is useful when you |
| 1176 | have a shell function with the same name as a builtin command.) |
| 1177 | .Bl -tag -width 5n |
| 1178 | .It Fl p |
| 1179 | search for command using a |
| 1180 | .Ev PATH |
| 1181 | that guarantees to find all the standard utilities. |
| 1182 | .It Fl V |
| 1183 | Do not execute the command but |
| 1184 | search for the command and print the resolution of the |
| 1185 | command search. |
| 1186 | This is the same as the type builtin. |
| 1187 | .It Fl v |
| 1188 | Do not execute the command but |
| 1189 | search for the command and print the absolute pathname |
| 1190 | of utilities, the name for builtins or the expansion of aliases. |
| 1191 | .El |
| 1192 | .It cd Op Ar directory Op Ar replace |
| 1193 | Switch to the specified directory (default |
| 1194 | .Ev $HOME ) . |
| 1195 | If |
| 1196 | .Ar replace |
| 1197 | is specified, then the new directory name is generated by replacing |
| 1198 | the first occurrence of |
| 1199 | .Ar directory |
| 1200 | in the current directory name with |
| 1201 | .Ar replace . |
| 1202 | Otherwise if an entry for |
| 1203 | .Ev CDPATH |
| 1204 | appears in the environment of the |
| 1205 | .Ic cd |
| 1206 | command or the shell variable |
| 1207 | .Ev CDPATH |
| 1208 | is set and the directory name does not begin with a slash, then the |
| 1209 | directories listed in |
| 1210 | .Ev CDPATH |
| 1211 | will be searched for the specified directory. |
| 1212 | The format of |
| 1213 | .Ev CDPATH |
| 1214 | is the same as that of |
| 1215 | .Ev PATH . |
| 1216 | In an interactive shell, the |
| 1217 | .Ic cd |
| 1218 | command will print out the name of the |
| 1219 | directory that it actually switched to if this is different from the name |
| 1220 | that the user gave. |
| 1221 | These may be different either because the |
| 1222 | .Ev CDPATH |
| 1223 | mechanism was used or because a symbolic link was crossed. |
| 1224 | .It eval Ar string ... |
| 1225 | Concatenate all the arguments with spaces. |
| 1226 | Then re-parse and execute the command. |
| 1227 | .It exec Op Ar command arg ... |
| 1228 | Unless command is omitted, the shell process is replaced with the |
| 1229 | specified program (which must be a real program, not a shell builtin or |
| 1230 | function). |
| 1231 | Any redirections on the |
| 1232 | .Ic exec |
| 1233 | command are marked as permanent, so that they are not undone when the |
| 1234 | .Ic exec |
| 1235 | command finishes. |
| 1236 | .It exit Op Ar exitstatus |
| 1237 | Terminate the shell process. |
| 1238 | If |
| 1239 | .Ar exitstatus |
| 1240 | is given it is used as the exit status of the shell; otherwise the |
| 1241 | exit status of the preceding command is used. |
| 1242 | .It export Ar name ... |
| 1243 | .It export Fl p |
| 1244 | The specified names are exported so that they will appear in the |
| 1245 | environment of subsequent commands. |
| 1246 | The only way to un-export a variable is to unset it. |
| 1247 | The shell allows the value of a variable to be set at the |
| 1248 | same time it is exported by writing |
| 1249 | .Pp |
| 1250 | .Dl export name=value |
| 1251 | .Pp |
| 1252 | With no arguments the export command lists the names of all exported variables. |
| 1253 | With the |
| 1254 | .Fl p |
| 1255 | option specified the output will be formatted suitably for non-interactive use. |
| 1256 | .It Xo fc Op Fl e Ar editor |
| 1257 | .Op Ar first Op Ar last |
| 1258 | .Xc |
| 1259 | .It Xo fc Fl l |
| 1260 | .Op Fl nr |
| 1261 | .Op Ar first Op Ar last |
| 1262 | .Xc |
| 1263 | .It Xo fc Fl s Op Ar old=new |
| 1264 | .Op Ar first |
| 1265 | .Xc |
| 1266 | The |
| 1267 | .Ic fc |
| 1268 | builtin lists, or edits and re-executes, commands previously entered |
| 1269 | to an interactive shell. |
| 1270 | .Bl -tag -width 5n |
| 1271 | .It Fl e No editor |
| 1272 | Use the editor named by editor to edit the commands. |
| 1273 | The editor string is a command name, subject to search via the |
| 1274 | .Ev PATH |
| 1275 | variable. |
| 1276 | The value in the |
| 1277 | .Ev FCEDIT |
| 1278 | variable is used as a default when |
| 1279 | .Fl e |
| 1280 | is not specified. |
| 1281 | If |
| 1282 | .Ev FCEDIT |
| 1283 | is null or unset, the value of the |
| 1284 | .Ev EDITOR |
| 1285 | variable is used. |
| 1286 | If |
| 1287 | .Ev EDITOR |
| 1288 | is null or unset, |
| 1289 | .Xr ed 1 |
| 1290 | is used as the editor. |
| 1291 | .It Fl l No (ell) |
| 1292 | List the commands rather than invoking an editor on them. |
| 1293 | The commands are written in the sequence indicated by |
| 1294 | the first and last operands, as affected by |
| 1295 | .Fl r , |
| 1296 | with each command preceded by the command number. |
| 1297 | .It Fl n |
| 1298 | Suppress command numbers when listing with -l. |
| 1299 | .It Fl r |
| 1300 | Reverse the order of the commands listed (with |
| 1301 | .Fl l ) |
| 1302 | or edited (with neither |
| 1303 | .Fl l |
| 1304 | nor |
| 1305 | .Fl s ) . |
| 1306 | .It Fl s |
| 1307 | Re-execute the command without invoking an editor. |
| 1308 | .It first |
| 1309 | .It last |
| 1310 | Select the commands to list or edit. |
| 1311 | The number of previous commands that |
| 1312 | can be accessed are determined by the value of the |
| 1313 | .Ev HISTSIZE |
| 1314 | variable. |
| 1315 | The value of first or last or both are one of the following: |
| 1316 | .Bl -tag -width 5n |
| 1317 | .It [+]number |
| 1318 | A positive number representing a command number; command numbers can be |
| 1319 | displayed with the |
| 1320 | .Fl l |
| 1321 | option. |
| 1322 | .It Fl number |
| 1323 | A negative decimal number representing the command that was executed |
| 1324 | number of commands previously. |
| 1325 | For example, \-1 is the immediately previous command. |
| 1326 | .El |
| 1327 | .It string |
| 1328 | A string indicating the most recently entered command that begins with |
| 1329 | that string. |
| 1330 | If the old=new operand is not also specified with |
| 1331 | .Fl s , |
| 1332 | the string form of the first operand cannot contain an embedded equal sign. |
| 1333 | .El |
| 1334 | .Pp |
| 1335 | The following environment variables affect the execution of fc: |
| 1336 | .Bl -tag -width HISTSIZE |
| 1337 | .It Ev FCEDIT |
| 1338 | Name of the editor to use. |
| 1339 | .It Ev HISTSIZE |
| 1340 | The number of previous commands that are accessible. |
| 1341 | .El |
| 1342 | .It fg Op Ar job |
| 1343 | Move the specified job or the current job to the foreground. |
| 1344 | .It getopts Ar optstring var |
| 1345 | The |
| 1346 | .Tn POSIX |
| 1347 | .Ic getopts |
| 1348 | command, not to be confused with the |
| 1349 | .Em Bell Labs |
| 1350 | -derived |
| 1351 | .Xr getopt 1 . |
| 1352 | .Pp |
| 1353 | The first argument should be a series of letters, each of which may be |
| 1354 | optionally followed by a colon to indicate that the option requires an |
| 1355 | argument. |
| 1356 | The variable specified is set to the parsed option. |
| 1357 | .Pp |
| 1358 | The |
| 1359 | .Ic getopts |
| 1360 | command deprecates the older |
| 1361 | .Xr getopt 1 |
| 1362 | utility due to its handling of arguments containing whitespace. |
| 1363 | .Pp |
| 1364 | The |
| 1365 | .Ic getopts |
| 1366 | builtin may be used to obtain options and their arguments |
| 1367 | from a list of parameters. |
| 1368 | When invoked, |
| 1369 | .Ic getopts |
| 1370 | places the value of the next option from the option string in the list in |
| 1371 | the shell variable specified by |
| 1372 | .Va var |
| 1373 | and its index in the shell variable |
| 1374 | .Ev OPTIND . |
| 1375 | When the shell is invoked, |
| 1376 | .Ev OPTIND |
| 1377 | is initialized to 1. |
| 1378 | For each option that requires an argument, the |
| 1379 | .Ic getopts |
| 1380 | builtin will place it in the shell variable |
| 1381 | .Ev OPTARG . |
| 1382 | If an option is not allowed for in the |
| 1383 | .Va optstring , |
| 1384 | then |
| 1385 | .Ev OPTARG |
| 1386 | will be unset. |
| 1387 | .Pp |
| 1388 | .Va optstring |
| 1389 | is a string of recognized option letters (see |
| 1390 | .Xr getopt 3 ) . |
| 1391 | If a letter is followed by a colon, the option is expected to have an |
| 1392 | argument which may or may not be separated from it by white space. |
| 1393 | If an option character is not found where expected, |
| 1394 | .Ic getopts |
| 1395 | will set the variable |
| 1396 | .Va var |
| 1397 | to a |
| 1398 | .Dq \&? ; |
| 1399 | .Ic getopts |
| 1400 | will then unset |
| 1401 | .Ev OPTARG |
| 1402 | and write output to standard error. |
| 1403 | By specifying a colon as the first character of |
| 1404 | .Va optstring |
| 1405 | all errors will be ignored. |
| 1406 | .Pp |
| 1407 | A nonzero value is returned when the last option is reached. |
| 1408 | If there are no remaining arguments, |
| 1409 | .Ic getopts |
| 1410 | will set |
| 1411 | .Va var |
| 1412 | to the special option, |
| 1413 | .Dq -- , |
| 1414 | otherwise, it will set |
| 1415 | .Va var |
| 1416 | to |
| 1417 | .Dq \&? . |
| 1418 | .Pp |
| 1419 | The following code fragment shows how one might process the arguments |
| 1420 | for a command that can take the options |
| 1421 | .Op a |
| 1422 | and |
| 1423 | .Op b , |
| 1424 | and the option |
| 1425 | .Op c , |
| 1426 | which requires an argument. |
| 1427 | .Pp |
| 1428 | .Bd -literal -offset indent |
| 1429 | while getopts abc: f |
| 1430 | do |
| 1431 | case $f in |
| 1432 | a | b) flag=$f;; |
| 1433 | c) carg=$OPTARG;; |
| 1434 | \\?) echo $USAGE; exit 1;; |
| 1435 | esac |
| 1436 | done |
| 1437 | shift `expr $OPTIND - 1` |
| 1438 | .Ed |
| 1439 | .Pp |
| 1440 | This code will accept any of the following as equivalent: |
| 1441 | .Pp |
| 1442 | .Bd -literal -offset indent |
| 1443 | cmd \-acarg file file |
| 1444 | cmd \-a \-c arg file file |
| 1445 | cmd \-carg -a file file |
| 1446 | cmd \-a \-carg \-\- file file |
| 1447 | .Ed |
| 1448 | .It hash Fl rv Ar command ... |
| 1449 | The shell maintains a hash table which remembers the |
| 1450 | locations of commands. |
| 1451 | With no arguments whatsoever, |
| 1452 | the |
| 1453 | .Ic hash |
| 1454 | command prints out the contents of this table. |
| 1455 | Entries which have not been looked at since the last |
| 1456 | .Ic cd |
| 1457 | command are marked with an asterisk; it is possible for these entries |
| 1458 | to be invalid. |
| 1459 | .Pp |
| 1460 | With arguments, the |
| 1461 | .Ic hash |
| 1462 | command removes the specified commands from the hash table (unless |
| 1463 | they are functions) and then locates them. |
| 1464 | With the |
| 1465 | .Fl v |
| 1466 | option, hash prints the locations of the commands as it finds them. |
| 1467 | The |
| 1468 | .Fl r |
| 1469 | option causes the hash command to delete all the entries in the hash table |
| 1470 | except for functions. |
| 1471 | .It inputrc Ar file |
| 1472 | Read the |
| 1473 | .Va file |
| 1474 | to set keybindings as defined by |
| 1475 | .Xr editrc 5 . |
| 1476 | .It jobid Op Ar job |
| 1477 | Print the process id's of the processes in the job. |
| 1478 | If the |
| 1479 | .Ar job |
| 1480 | argument is omitted, the current job is used. |
| 1481 | .It jobs |
| 1482 | This command lists out all the background processes |
| 1483 | which are children of the current shell process. |
| 1484 | .It pwd Op Fl LP |
| 1485 | Print the current directory. |
| 1486 | If |
| 1487 | .Fl L |
| 1488 | is specified the cached value (initially set from |
| 1489 | .Ev PWD ) |
| 1490 | is checked to see if it refers to the current directory, if it does |
| 1491 | the value is printed. |
| 1492 | Otherwise the current directory name is found using |
| 1493 | .Xr getcwd(3) . |
| 1494 | The environment variable |
| 1495 | .Ev PWD |
| 1496 | is set to printed value. |
| 1497 | .Pp |
| 1498 | The default is |
| 1499 | .Ic pwd |
| 1500 | .Fl L , |
| 1501 | but note that the builtin |
| 1502 | .Ic cd |
| 1503 | command doesn't currently support |
| 1504 | .Fl L |
| 1505 | or |
| 1506 | .Fl P |
| 1507 | and will cache (almost) the absolute path. |
| 1508 | If |
| 1509 | .Ic cd |
| 1510 | is changed, |
| 1511 | .Ic pwd |
| 1512 | may be changed to default to |
| 1513 | .Ic pwd |
| 1514 | .Fl P . |
| 1515 | .Pp |
| 1516 | If the current directory is renamed and replaced by a symlink to the |
| 1517 | same directory, or the initial |
| 1518 | .Ev PWD |
| 1519 | value followed a symbolic link, then the cached value may not |
| 1520 | be the absolute path. |
| 1521 | .Pp |
| 1522 | The builtin command may differ from the program of the same name because |
| 1523 | the program will use |
| 1524 | .Ev PWD |
| 1525 | and the builtin uses a separately cached value. |
| 1526 | .It Xo read Op Fl p Ar prompt |
| 1527 | .Op Fl r |
| 1528 | .Ar variable |
| 1529 | .Op Ar ... |
| 1530 | .Xc |
| 1531 | The prompt is printed if the |
| 1532 | .Fl p |
| 1533 | option is specified and the standard input is a terminal. |
| 1534 | Then a line is read from the standard input. |
| 1535 | The trailing newline is deleted from the |
| 1536 | line and the line is split as described in the section on word splitting |
| 1537 | above, and the pieces are assigned to the variables in order. |
| 1538 | If there are more pieces than variables, the remaining pieces |
| 1539 | (along with the characters in |
| 1540 | .Ev IFS |
| 1541 | that separated them) are assigned to the last variable. |
| 1542 | If there are more variables than pieces, |
| 1543 | the remaining variables are assigned the null string. |
| 1544 | The |
| 1545 | .Ic read |
| 1546 | builtin will indicate success unless EOF is encountered on input, in |
| 1547 | which case failure is returned. |
| 1548 | .Pp |
| 1549 | By default, unless the |
| 1550 | .Fl r |
| 1551 | option is specified, the backslash |
| 1552 | .Dq \e |
| 1553 | acts as an escape character, causing the following character to be treated |
| 1554 | literally. |
| 1555 | If a backslash is followed by a newline, the backslash and the |
| 1556 | newline will be deleted. |
| 1557 | .It readonly Ar name ... |
| 1558 | .It readonly Fl p |
| 1559 | The specified names are marked as read only, so that they cannot be |
| 1560 | subsequently modified or unset. |
| 1561 | The shell allows the value of a variable |
| 1562 | to be set at the same time it is marked read only by writing |
| 1563 | .Pp |
| 1564 | .Dl readonly name=value |
| 1565 | .Pp |
| 1566 | With no arguments the readonly command lists the names of all read only |
| 1567 | variables. |
| 1568 | With the |
| 1569 | .Fl p |
| 1570 | option specified the output will be formatted suitably for non-interactive use. |
| 1571 | .Pp |
| 1572 | .It Xo set |
| 1573 | .Oo { |
| 1574 | .Fl options | Cm +options | Cm -- } |
| 1575 | .Oc Ar arg ... |
| 1576 | .Xc |
| 1577 | The |
| 1578 | .Ic set |
| 1579 | command performs three different functions. |
| 1580 | .Pp |
| 1581 | With no arguments, it lists the values of all shell variables. |
| 1582 | .Pp |
| 1583 | If options are given, it sets the specified option |
| 1584 | flags, or clears them as described in the section called |
| 1585 | .Sx Argument List Processing . |
| 1586 | .Pp |
| 1587 | The third use of the set command is to set the values of the shell's |
| 1588 | positional parameters to the specified args. |
| 1589 | To change the positional |
| 1590 | parameters without changing any options, use |
| 1591 | .Dq -- |
| 1592 | as the first argument to set. |
| 1593 | If no args are present, the set command |
| 1594 | will clear all the positional parameters (equivalent to executing |
| 1595 | .Dq shift $# . ) |
| 1596 | .It setvar Ar variable Ar value |
| 1597 | Assigns value to variable. |
| 1598 | (In general it is better to write |
| 1599 | variable=value rather than using |
| 1600 | .Ic setvar . |
| 1601 | .Ic setvar |
| 1602 | is intended to be used in |
| 1603 | functions that assign values to variables whose names are passed as |
| 1604 | parameters.) |
| 1605 | .It shift Op Ar n |
| 1606 | Shift the positional parameters n times. |
| 1607 | A |
| 1608 | .Ic shift |
| 1609 | sets the value of |
| 1610 | .Va $1 |
| 1611 | to the value of |
| 1612 | .Va $2 , |
| 1613 | the value of |
| 1614 | .Va $2 |
| 1615 | to the value of |
| 1616 | .Va $3 , |
| 1617 | and so on, decreasing |
| 1618 | the value of |
| 1619 | .Va $# |
| 1620 | by one. |
| 1621 | If there are zero positional parameters, |
| 1622 | .Ic shift |
| 1623 | does nothing. |
| 1624 | .It Xo trap |
| 1625 | .Op Fl l |
| 1626 | .Xc |
| 1627 | .It Xo trap |
| 1628 | .Op Ar action |
| 1629 | .Ar signal ... |
| 1630 | .Xc |
| 1631 | Cause the shell to parse and execute action when any of the specified |
| 1632 | signals are received. |
| 1633 | The signals are specified by signal number or as the name of the signal. |
| 1634 | If |
| 1635 | .Ar signal |
| 1636 | is |
| 1637 | .Li 0 , |
| 1638 | the action is executed when the shell exits. |
| 1639 | .Ar action |
| 1640 | may be null, which cause the specified signals to be ignored. |
| 1641 | With |
| 1642 | .Ar action |
| 1643 | omitted or set to `-' the specified signals are set to their default action. |
| 1644 | When the shell forks off a subshell, it resets trapped (but not ignored) |
| 1645 | signals to the default action. |
| 1646 | The |
| 1647 | .Ic trap |
| 1648 | command has no effect on signals that were |
| 1649 | ignored on entry to the shell. |
| 1650 | Issuing |
| 1651 | .Ic trap |
| 1652 | with option |
| 1653 | .Ar -l |
| 1654 | will print a list of valid signal names. |
| 1655 | .Ic trap |
| 1656 | without any arguments cause it to write a list of signals and their |
| 1657 | associated action to the standard output in a format that is suitable |
| 1658 | as an input to the shell that achieves the same trapping results. |
| 1659 | .Pp |
| 1660 | Examples: |
| 1661 | .Pp |
| 1662 | .Dl trap |
| 1663 | .Pp |
| 1664 | List trapped signals and their corresponding action |
| 1665 | .Pp |
| 1666 | .Dl trap -l |
| 1667 | .Pp |
| 1668 | Print a list of valid signals |
| 1669 | .Pp |
| 1670 | .Dl trap '' INT QUIT tstp 30 |
| 1671 | .Pp |
| 1672 | Ignore signals INT QUIT TSTP USR1 |
| 1673 | .Pp |
| 1674 | .Dl trap date INT |
| 1675 | .Pp |
| 1676 | Print date upon receiving signal INT |
| 1677 | .It type Op Ar name ... |
| 1678 | Interpret each name as a command and print the resolution of the command |
| 1679 | search. |
| 1680 | Possible resolutions are: |
| 1681 | shell keyword, alias, shell builtin, |
| 1682 | command, tracked alias and not found. |
| 1683 | For aliases the alias expansion is |
| 1684 | printed; for commands and tracked aliases the complete pathname of the |
| 1685 | command is printed. |
| 1686 | .It ulimit Xo |
| 1687 | .Op Fl H \*(Ba Fl S |
| 1688 | .Op Fl a \*(Ba Fl tfdscmlpn Op Ar value |
| 1689 | .Xc |
| 1690 | Inquire about or set the hard or soft limits on processes or set new |
| 1691 | limits. |
| 1692 | The choice between hard limit (which no process is allowed to |
| 1693 | violate, and which may not be raised once it has been lowered) and soft |
| 1694 | limit (which causes processes to be signaled but not necessarily killed, |
| 1695 | and which may be raised) is made with these flags: |
| 1696 | .Bl -tag -width Fl |
| 1697 | .It Fl H |
| 1698 | set or inquire about hard limits |
| 1699 | .It Fl S |
| 1700 | set or inquire about soft limits. |
| 1701 | If neither |
| 1702 | .Fl H |
| 1703 | nor |
| 1704 | .Fl S |
| 1705 | is specified, the soft limit is displayed or both limits are set. |
| 1706 | If both are specified, the last one wins. |
| 1707 | .El |
| 1708 | .Pp |
| 1709 | .Bl -tag -width Fl |
| 1710 | The limit to be interrogated or set, then, is chosen by specifying |
| 1711 | any one of these flags: |
| 1712 | .It Fl a |
| 1713 | show all the current limits |
| 1714 | .It Fl b |
| 1715 | show or set the limit on the socket buffer size of a process (in bytes) |
| 1716 | .It Fl t |
| 1717 | show or set the limit on CPU time (in seconds) |
| 1718 | .It Fl f |
| 1719 | show or set the limit on the largest file that can be created |
| 1720 | (in 512-byte blocks) |
| 1721 | .It Fl d |
| 1722 | show or set the limit on the data segment size of a process (in kilobytes) |
| 1723 | .It Fl s |
| 1724 | show or set the limit on the stack size of a process (in kilobytes) |
| 1725 | .It Fl c |
| 1726 | show or set the limit on the largest core dump size that can be produced |
| 1727 | (in 512-byte blocks) |
| 1728 | .It Fl m |
| 1729 | show or set the limit on the total physical memory that can be |
| 1730 | in use by a process (in kilobytes) |
| 1731 | .It Fl l |
| 1732 | show or set the limit on how much memory a process can lock with |
| 1733 | .Xr mlock 2 |
| 1734 | (in kilobytes) |
| 1735 | .It Fl p |
| 1736 | show or set the limit on the number of processes this user can |
| 1737 | have at one time |
| 1738 | .It Fl n |
| 1739 | show or set the limit on the number of files a process can have open at once |
| 1740 | .El |
| 1741 | .Pp |
| 1742 | If none of these is specified, it is the limit on file size that is shown |
| 1743 | or set. |
| 1744 | If value is specified, the limit is set to that number; otherwise |
| 1745 | the current limit is displayed. |
| 1746 | .Pp |
| 1747 | Limits of an arbitrary process can be displayed or set using the |
| 1748 | .Xr sysctl 8 |
| 1749 | utility. |
| 1750 | .Pp |
| 1751 | .It umask Op Ar mask |
| 1752 | Set the value of umask (see |
| 1753 | .Xr umask 2 ) |
| 1754 | to the specified octal value. |
| 1755 | If the argument is omitted, the umask value is printed. |
| 1756 | .It unalias Xo |
| 1757 | .Op Fl a |
| 1758 | .Op Ar name |
| 1759 | .Xc |
| 1760 | If |
| 1761 | .Ar name |
| 1762 | is specified, the shell removes that alias. |
| 1763 | If |
| 1764 | .Fl a |
| 1765 | is specified, all aliases are removed. |
| 1766 | .It unset Ar name ... |
| 1767 | The specified variables and functions are unset and unexported. |
| 1768 | If a given name corresponds to both a variable and a function, both |
| 1769 | the variable and the function are unset. |
| 1770 | .It wait Op Ar job |
| 1771 | Wait for the specified job to complete and return the exit status of the |
| 1772 | last process in the job. |
| 1773 | If the argument is omitted, wait for all jobs to |
| 1774 | complete and then return an exit status of zero. |
| 1775 | .El |
| 1776 | .Ss Command Line Editing |
| 1777 | When |
| 1778 | .Nm |
| 1779 | is being used interactively from a terminal, the current command |
| 1780 | and the command history (see |
| 1781 | .Ic fc |
| 1782 | in |
| 1783 | .Sx Builtins ) |
| 1784 | can be edited using emacs-mode or vi-mode command-line editing. |
| 1785 | The command |
| 1786 | .Ql set -o emacs |
| 1787 | enables emacs-mode editing. |
| 1788 | The command |
| 1789 | .Ql set -o vi |
| 1790 | enables vi-mode editing and places sh into vi insert mode. |
| 1791 | (See the |
| 1792 | .Sx Argument List Processing |
| 1793 | section above.) |
| 1794 | .Pp |
| 1795 | The vi mode uses commands similar to a subset of those described in the |
| 1796 | .Xr vi 1 |
| 1797 | man page. |
| 1798 | With vi-mode |
| 1799 | enabled, sh can be switched between insert mode and command mode. |
| 1800 | It's similar to vi: typing |
| 1801 | .Aq ESC |
| 1802 | will throw you into command VI command mode. |
| 1803 | Hitting |
| 1804 | .Aq return |
| 1805 | while in command mode will pass the line to the shell. |
| 1806 | .Pp |
| 1807 | The emacs mode uses commands similar to a subset available in |
| 1808 | the emacs editor. |
| 1809 | With emacs-mode enabled, special keys can be used to modify the text |
| 1810 | in the buffer using the control key. |
| 1811 | .Pp |
| 1812 | .Nm |
| 1813 | uses the |
| 1814 | .Xr editline 3 |
| 1815 | library. |
| 1816 | .Sh EXIT STATUS |
| 1817 | Errors that are detected by the shell, such as a syntax error, will cause the |
| 1818 | shell to exit with a non-zero exit status. |
| 1819 | If the shell is not an |
| 1820 | interactive shell, the execution of the shell file will be aborted. |
| 1821 | Otherwise |
| 1822 | the shell will return the exit status of the last command executed, or |
| 1823 | if the exit builtin is used with a numeric argument, it will return the |
| 1824 | argument. |
| 1825 | .Sh ENVIRONMENT |
| 1826 | .Bl -tag -width MAILCHECK |
| 1827 | .It Ev HOME |
| 1828 | Set automatically by |
| 1829 | .Xr login 1 |
| 1830 | from the user's login directory in the password file |
| 1831 | .Pq Xr passwd 5 . |
| 1832 | This environment variable also functions as the default argument for the |
| 1833 | cd builtin. |
| 1834 | .It Ev PATH |
| 1835 | The default search path for executables. |
| 1836 | See the above section |
| 1837 | .Sx Path Search . |
| 1838 | .It Ev CDPATH |
| 1839 | The search path used with the cd builtin. |
| 1840 | .It Ev LANG |
| 1841 | The string used to specify localization information that allows users |
| 1842 | to work with different culture-specific and language conventions. |
| 1843 | See |
| 1844 | .Xr nls 7 . |
| 1845 | .It Ev MAIL |
| 1846 | The name of a mail file, that will be checked for the arrival of new mail. |
| 1847 | Overridden by |
| 1848 | .Ev MAILPATH . |
| 1849 | .It Ev MAILCHECK |
| 1850 | The frequency in seconds that the shell checks for the arrival of mail |
| 1851 | in the files specified by the |
| 1852 | .Ev MAILPATH |
| 1853 | or the |
| 1854 | .Ev MAIL |
| 1855 | file. |
| 1856 | If set to 0, the check will occur at each prompt. |
| 1857 | .It Ev MAILPATH |
| 1858 | A colon |
| 1859 | .Dq \&: |
| 1860 | separated list of file names, for the shell to check for incoming mail. |
| 1861 | This environment setting overrides the |
| 1862 | .Ev MAIL |
| 1863 | setting. |
| 1864 | There is a maximum of 10 mailboxes that can be monitored at once. |
| 1865 | .It Ev PS1 |
| 1866 | The primary prompt string, which defaults to |
| 1867 | .Dq $ \ , |
| 1868 | unless you are the superuser, in which case it defaults to |
| 1869 | .Dq # \ . |
| 1870 | .It Ev PS2 |
| 1871 | The secondary prompt string, which defaults to |
| 1872 | .Dq \*[Gt] \ . |
| 1873 | .It Ev PS4 |
| 1874 | Output before each line when execution trace (set -x) is enabled, |
| 1875 | defaults to |
| 1876 | .Dq + \ . |
| 1877 | .It Ev IFS |
| 1878 | Input Field Separators. |
| 1879 | This is normally set to |
| 1880 | .Aq space , |
| 1881 | .Aq tab , |
| 1882 | and |
| 1883 | .Aq newline . |
| 1884 | See the |
| 1885 | .Sx White Space Splitting |
| 1886 | section for more details. |
| 1887 | .It Ev TERM |
| 1888 | The default terminal setting for the shell. |
| 1889 | This is inherited by |
| 1890 | children of the shell, and is used in the history editing modes. |
| 1891 | .It Ev HISTSIZE |
| 1892 | The number of lines in the history buffer for the shell. |
| 1893 | .El |
| 1894 | .Sh FILES |
| 1895 | .Bl -item -width HOMEprofilexxxx |
| 1896 | .It |
| 1897 | .Pa $HOME/.profile |
| 1898 | .It |
| 1899 | .Pa /etc/profile |
| 1900 | .El |
| 1901 | .Sh SEE ALSO |
| 1902 | .Xr csh 1 , |
| 1903 | .Xr echo 1 , |
| 1904 | .Xr getopt 1 , |
| 1905 | .Xr ksh 1 , |
| 1906 | .Xr login 1 , |
| 1907 | .Xr printf 1 , |
| 1908 | .Xr test 1 , |
| 1909 | .Xr editline 3 , |
| 1910 | .Xr getopt 3 , |
| 1911 | .\" .Xr profile 4 , |
| 1912 | .Xr editrc 5 , |
| 1913 | .Xr passwd 5 , |
| 1914 | .Xr environ 7 , |
| 1915 | .Xr nls 7 , |
| 1916 | .Xr sysctl 8 |
| 1917 | .Sh HISTORY |
| 1918 | A |
| 1919 | .Nm |
| 1920 | command appeared in |
| 1921 | .At v1 . |
| 1922 | It was, however, unmaintainable so we wrote this one. |
| 1923 | .Sh BUGS |
| 1924 | Setuid shell scripts should be avoided at all costs, as they are a |
| 1925 | significant security risk. |
| 1926 | .Pp |
| 1927 | PS1, PS2, and PS4 should be subject to parameter expansion before |
| 1928 | being displayed. |