[9] | 1 | .\" Copyright (c) 1991 The Regents of the University of California.
|
---|
| 2 | .\" All rights reserved.
|
---|
| 3 | .\"
|
---|
| 4 | .\" This code is derived from software contributed to Berkeley by
|
---|
| 5 | .\" Kenneth Almquist.
|
---|
| 6 | .\"
|
---|
| 7 | .\" Redistribution and use in source and binary forms, with or without
|
---|
| 8 | .\" modification, are permitted provided that the following conditions
|
---|
| 9 | .\" are met:
|
---|
| 10 | .\" 1. Redistributions of source code must retain the above copyright
|
---|
| 11 | .\" notice, this list of conditions and the following disclaimer.
|
---|
| 12 | .\" 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | .\" notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | .\" documentation and/or other materials provided with the distribution.
|
---|
| 15 | .\" 3. All advertising materials mentioning features or use of this software
|
---|
| 16 | .\" must display the following acknowledgement:
|
---|
| 17 | .\" This product includes software developed by the University of
|
---|
| 18 | .\" California, Berkeley and its contributors.
|
---|
| 19 | .\" 4. Neither the name of the University nor the names of its contributors
|
---|
| 20 | .\" may be used to endorse or promote products derived from this software
|
---|
| 21 | .\" without specific prior written permission.
|
---|
| 22 | .\"
|
---|
| 23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 33 | .\" SUCH DAMAGE.
|
---|
| 34 | .\"
|
---|
| 35 | .\" @(#)sh.1 5.1 (Berkeley) 3/7/91
|
---|
| 36 | .\"
|
---|
| 37 | .TH SH 1 "March 7, 1991"
|
---|
| 38 | .UC 7
|
---|
| 39 | .de h \" subheading
|
---|
| 40 | .sp
|
---|
| 41 | .ti -0.3i
|
---|
| 42 | .B "\\$1"
|
---|
| 43 | .PP
|
---|
| 44 | ..
|
---|
| 45 | .de d \" begin display
|
---|
| 46 | .sp
|
---|
| 47 | .in +4
|
---|
| 48 | .nf
|
---|
| 49 | ..
|
---|
| 50 | .de e \" end display
|
---|
| 51 | .in -4
|
---|
| 52 | .fi
|
---|
| 53 | .sp
|
---|
| 54 | ..
|
---|
| 55 | .de c \" command, etc.
|
---|
| 56 | .br
|
---|
| 57 | .HP 3
|
---|
| 58 | \fB\\$1\fR
|
---|
| 59 | .br
|
---|
| 60 | ..
|
---|
| 61 | .de b \" begin builtin command
|
---|
| 62 | .HP 3
|
---|
| 63 | .B \\$1
|
---|
| 64 | ..
|
---|
| 65 | .SH NAME
|
---|
| 66 | ash, sh, ., break, case, cd, command, continue, eval, exec, exit, export, for, getopts, hash, if, jobs, local, read, readonly, return, set, setvar, shift, trap, umask, unset, wait, while \- a shell
|
---|
| 67 | .SH SYNOPSIS
|
---|
| 68 | .B ash
|
---|
| 69 | [
|
---|
| 70 | .B -efIijnsxz
|
---|
| 71 | ] [
|
---|
| 72 | .B +efIijnsxz
|
---|
| 73 | ] [
|
---|
| 74 | .B -c
|
---|
| 75 | .I command
|
---|
| 76 | ] [
|
---|
| 77 | .I arg
|
---|
| 78 | ] ...
|
---|
| 79 | .SH COPYRIGHT
|
---|
| 80 | Copyright 1989 by Kenneth Almquist.
|
---|
| 81 | .SH DESCRIPTION
|
---|
| 82 | .I Ash
|
---|
| 83 | is a version of
|
---|
| 84 | .I sh
|
---|
| 85 | with features similar to those of the System V shell.
|
---|
| 86 | This manual page lists all the features of
|
---|
| 87 | .I ash
|
---|
| 88 | but concentrates on the ones not in other shells.
|
---|
| 89 | .h "Invocation"
|
---|
| 90 | If the
|
---|
| 91 | .B -c
|
---|
| 92 | options is given, then the shell executes the specified shell command.
|
---|
| 93 | The
|
---|
| 94 | .B -s
|
---|
| 95 | flag cause the shell to read commands from the standard input (after
|
---|
| 96 | executing any command specified with the
|
---|
| 97 | .B -c
|
---|
| 98 | option.
|
---|
| 99 | If neither the
|
---|
| 100 | .B -s
|
---|
| 101 | or
|
---|
| 102 | .B -c
|
---|
| 103 | options are set, then the first
|
---|
| 104 | .I arg
|
---|
| 105 | is taken as the name of a file to read commands from.
|
---|
| 106 | If this is impossible because there are no arguments following
|
---|
| 107 | the options, then
|
---|
| 108 | .I ash
|
---|
| 109 | will set the
|
---|
| 110 | .B -s
|
---|
| 111 | flag and will read commands from the standard input.
|
---|
| 112 | .PP
|
---|
| 113 | The shell sets the initial value of the positional parameters from the
|
---|
| 114 | .IR arg s
|
---|
| 115 | remaining after any
|
---|
| 116 | .I arg
|
---|
| 117 | used as the name of a file of commands is deleted.
|
---|
| 118 | .PP
|
---|
| 119 | The flags (other than
|
---|
| 120 | .BR -c )
|
---|
| 121 | are set by preceding them with ``-'' and cleared by preceding them
|
---|
| 122 | with ``+''; see the
|
---|
| 123 | .I set
|
---|
| 124 | builtin command for a list of flags.
|
---|
| 125 | If no value is specified for the
|
---|
| 126 | .B -i
|
---|
| 127 | flag, the
|
---|
| 128 | .B -s
|
---|
| 129 | flag is set, and the standard input and output of the shell
|
---|
| 130 | are connected to terminals, then the
|
---|
| 131 | .B -i
|
---|
| 132 | flag will be set.
|
---|
| 133 | If no value is specified for the
|
---|
| 134 | .B -j
|
---|
| 135 | flag, then the
|
---|
| 136 | .B -j
|
---|
| 137 | flag will be set if the
|
---|
| 138 | .B -i
|
---|
| 139 | flag is set.
|
---|
| 140 | .PP
|
---|
| 141 | When the shell is invoked with the
|
---|
| 142 | .B -c
|
---|
| 143 | option, it is good practice to include the
|
---|
| 144 | .I -i
|
---|
| 145 | flag if the command was entered interactively by a user.
|
---|
| 146 | For compatibility with the System V shell, the
|
---|
| 147 | .I -i
|
---|
| 148 | option should come after the
|
---|
| 149 | .B -c
|
---|
| 150 | option.
|
---|
| 151 | .PP
|
---|
| 152 | If the first character of argument zero to the shell is ``-'',
|
---|
| 153 | the shell is assumed to be a login shell, and the files
|
---|
| 154 | .B /etc/profile
|
---|
| 155 | and
|
---|
| 156 | .B .profile
|
---|
| 157 | are read if they exist.
|
---|
| 158 | If the environment variable SHINIT is set on entry to the shell,
|
---|
| 159 | the commands in SHINIT are normally parsed and executed. SHINIT is
|
---|
| 160 | not examined if the shell is a login shell, or if it the shell is running a
|
---|
| 161 | shell procedure. (A shell is considered to be running a shell
|
---|
| 162 | procedure if neither the
|
---|
| 163 | .B -s
|
---|
| 164 | nor the
|
---|
| 165 | .B -c
|
---|
| 166 | options are set.)
|
---|
| 167 | .h "Control Structures"
|
---|
| 168 | A
|
---|
| 169 | .I list
|
---|
| 170 | is a sequence of zero or more commands separated by newlines,
|
---|
| 171 | semicolons, or ampersands, and optionally terminated by one of these
|
---|
| 172 | three characters. (This differs from the System V shell, which
|
---|
| 173 | requires a list to contain at least one command in most cases.) The
|
---|
| 174 | commands in a list are executed in the order they are written.
|
---|
| 175 | If command is followed by an ampersand, the shell starts the command
|
---|
| 176 | and immediately proceed onto the next command; otherwise it waits
|
---|
| 177 | for the command to terminate before proceeding to the next one.
|
---|
| 178 | .PP
|
---|
| 179 | ``&&'' and ``||'' are binary operators.
|
---|
| 180 | ``&&'' executes the first command, and then executes the second command
|
---|
| 181 | iff the exit status of the first command is zero. ``||'' is similar,
|
---|
| 182 | but executes the second command iff the exit status of the first command
|
---|
| 183 | is nonzero. ``&&'' and ``||'' both have the same priority.
|
---|
| 184 | .PP
|
---|
| 185 | The ``|'' operator is a binary operator which feeds the standard output
|
---|
| 186 | of the first command into the standard input of the second command.
|
---|
| 187 | The exit status of the ``|'' operator is the exit status of the second
|
---|
| 188 | command. ``|'' has a higher priority than ``||'' or ``&&''.
|
---|
| 189 | .PP
|
---|
| 190 | An
|
---|
| 191 | .I if
|
---|
| 192 | command looks like
|
---|
| 193 | .d
|
---|
| 194 | \fBif\fR list
|
---|
| 195 | \fBthen\fR list
|
---|
| 196 | .ti -\w'[ 'u
|
---|
| 197 | [ \fBelif\fR list
|
---|
| 198 | \fBthen\fR list ] ...
|
---|
| 199 | .ti -\w'[ 'u
|
---|
| 200 | [ \fBelse\fR list ]
|
---|
| 201 | \fBfi\fR
|
---|
| 202 | .e
|
---|
| 203 | .PP
|
---|
| 204 | A
|
---|
| 205 | .I while
|
---|
| 206 | command looks like
|
---|
| 207 | .d
|
---|
| 208 | \fBwhile\fR list
|
---|
| 209 | \fBdo\fR list
|
---|
| 210 | \fBdone\fR
|
---|
| 211 | .e
|
---|
| 212 | The two lists are executed repeatedly while the exit status of the first
|
---|
| 213 | list is zero. The
|
---|
| 214 | .I until
|
---|
| 215 | command is similar, but has the word
|
---|
| 216 | .B until
|
---|
| 217 | in place of
|
---|
| 218 | .B while
|
---|
| 219 | repeats until the exit status of the first list
|
---|
| 220 | is zero.
|
---|
| 221 | .PP
|
---|
| 222 | The
|
---|
| 223 | .I for
|
---|
| 224 | command looks like
|
---|
| 225 | .d
|
---|
| 226 | \fBfor\fR variable \fBin\fR word...
|
---|
| 227 | \fBdo\fR list
|
---|
| 228 | \fBdone\fR
|
---|
| 229 | .e
|
---|
| 230 | The words are expanded, and then the list is executed repeatedly with
|
---|
| 231 | the variable set to each word in turn.
|
---|
| 232 | .B do
|
---|
| 233 | and
|
---|
| 234 | .B done
|
---|
| 235 | may be replaced with
|
---|
| 236 | ``{'' and ``}''.
|
---|
| 237 | .PP
|
---|
| 238 | The
|
---|
| 239 | .I break
|
---|
| 240 | and
|
---|
| 241 | .I continue
|
---|
| 242 | commands look like
|
---|
| 243 | .d
|
---|
| 244 | \fBbreak\fR [ num ]
|
---|
| 245 | \fBcontinue\fR [ num ]
|
---|
| 246 | .e
|
---|
| 247 | .I Break
|
---|
| 248 | terminates the
|
---|
| 249 | .I num
|
---|
| 250 | innermost
|
---|
| 251 | .I for
|
---|
| 252 | or
|
---|
| 253 | .I while
|
---|
| 254 | loops.
|
---|
| 255 | .I Continue
|
---|
| 256 | continues with the next iteration of the
|
---|
| 257 | .IRnum'th
|
---|
| 258 | innermost loop.
|
---|
| 259 | These are implemented as builtin commands.
|
---|
| 260 | .PP
|
---|
| 261 | The
|
---|
| 262 | .I case
|
---|
| 263 | command looks like
|
---|
| 264 | .d
|
---|
| 265 | \fBcase\fR word \fBin\fR
|
---|
| 266 | pattern\fB)\fR list \fB;;\fR
|
---|
| 267 | \&...
|
---|
| 268 | \fBesac\fR
|
---|
| 269 | .e
|
---|
| 270 | The pattern can actually be one or more patterns (see
|
---|
| 271 | .I Patterns
|
---|
| 272 | below), separated by ``|'' characters.
|
---|
| 273 | .PP
|
---|
| 274 | Commands may be grouped by writing either
|
---|
| 275 | .d
|
---|
| 276 | \fB(\fRlist\fB)\fR
|
---|
| 277 | .e
|
---|
| 278 | or
|
---|
| 279 | .d
|
---|
| 280 | \fB{\fR list; \fB}\fR
|
---|
| 281 | .e
|
---|
| 282 | The first of these executes the commands in a subshell.
|
---|
| 283 | .PP
|
---|
| 284 | A function definition looks like
|
---|
| 285 | .d
|
---|
| 286 | name \fB( )\fR command
|
---|
| 287 | .e
|
---|
| 288 | A function definition is an executable statement; when executed it installs
|
---|
| 289 | a function named
|
---|
| 290 | .B name
|
---|
| 291 | and returns an exit status of zero.
|
---|
| 292 | The command is normally a list enclosed between ``{'' and ``}''.
|
---|
| 293 | .PP
|
---|
| 294 | Variables may be declared to be local to a function by using a
|
---|
| 295 | .I local
|
---|
| 296 | command. This should appear as the first staement of a function,
|
---|
| 297 | and looks like
|
---|
| 298 | .d
|
---|
| 299 | \fBlocal\fR [ variable | \fB-\fR ] ...
|
---|
| 300 | .e
|
---|
| 301 | .I Local
|
---|
| 302 | is implemented as a builtin command.
|
---|
| 303 | .PP
|
---|
| 304 | When a variable is made local, it inherits the initial value and
|
---|
| 305 | exported and readonly flags from the variable with the same name in the
|
---|
| 306 | surrounding scope, if there is one. Otherwise, the variable is
|
---|
| 307 | initially unset.
|
---|
| 308 | .I Ash
|
---|
| 309 | uses dynamic scoping, so that if you make the variable
|
---|
| 310 | .B x
|
---|
| 311 | local to function
|
---|
| 312 | .IR f ,
|
---|
| 313 | which then calls function
|
---|
| 314 | .IR g ,
|
---|
| 315 | references to the variable
|
---|
| 316 | .B x
|
---|
| 317 | made inside
|
---|
| 318 | .I g
|
---|
| 319 | will refer to the variable
|
---|
| 320 | .B x
|
---|
| 321 | declared inside
|
---|
| 322 | .IR f ,
|
---|
| 323 | not to the global variable named
|
---|
| 324 | .BR x .
|
---|
| 325 | .PP
|
---|
| 326 | The only special parameter that can be made local is ``\fB-\fR''.
|
---|
| 327 | Making ``\fB-\fR'' local any shell options that are changed via the
|
---|
| 328 | .I set
|
---|
| 329 | command inside the function to be restored to their original values
|
---|
| 330 | when the function returns.
|
---|
| 331 | .PP
|
---|
| 332 | The
|
---|
| 333 | .I return
|
---|
| 334 | command looks like
|
---|
| 335 | .d
|
---|
| 336 | \fBreturn\fR [ exitstatus ]
|
---|
| 337 | .e
|
---|
| 338 | It terminates the currently executing function.
|
---|
| 339 | .I Return
|
---|
| 340 | is implemented as a builtin command.
|
---|
| 341 | .h "Simple Commands"
|
---|
| 342 | A simple command is a sequence of words. The execution of a simple
|
---|
| 343 | command proceeds as follows. First, the leading words of the form
|
---|
| 344 | ``name=value'' are stripped off and assigned to the environment of
|
---|
| 345 | the command. Second, the words are expanded. Third, the first
|
---|
| 346 | remaining word is taken as the command name that command is located.
|
---|
| 347 | Fourth, any redirections are performed. Fifth, the command is
|
---|
| 348 | executed. We look at these operations in reverse order.
|
---|
| 349 | .PP
|
---|
| 350 | The execution of the command varies with the type of command.
|
---|
| 351 | There are three types of commands: shell functions, builtin commands,
|
---|
| 352 | and normal programs.
|
---|
| 353 | .PP
|
---|
| 354 | When a shell function is executed, all of the shell positional parameters
|
---|
| 355 | (except $0, which remains unchanged) are set to the parameters to the shell
|
---|
| 356 | function. The variables which are explicitly placed in the environment
|
---|
| 357 | of the command (by placing assignments to them before the function name)
|
---|
| 358 | are made local to the function and are set to values given.
|
---|
| 359 | Then the command given in the function definition is executed.
|
---|
| 360 | The positional parameters are restored to their original values when
|
---|
| 361 | the command completes.
|
---|
| 362 | .PP
|
---|
| 363 | Shell builtins are executed internally to the shell, without spawning
|
---|
| 364 | a new process.
|
---|
| 365 | .PP
|
---|
| 366 | When a normal program is executed, the shell runs the program, passing
|
---|
| 367 | the parameters and the environment to the program. If the program is
|
---|
| 368 | a shell procedure, the shell will interpret the program in a subshell.
|
---|
| 369 | The shell will reinitialize itself in this case, so that the effect
|
---|
| 370 | will be as if a new shell had been invoked to handle the shell procedure,
|
---|
| 371 | except that the location of commands located in the parent shell will
|
---|
| 372 | be remembered by the child. If the program is a file beginning with
|
---|
| 373 | ``#!'', the remainder of the first line specifies an interpreter for
|
---|
| 374 | the program. The shell (or the operating system, under Berkeley UNIX)
|
---|
| 375 | will run the interpreter in this case. The arguments to the interpreter
|
---|
| 376 | will consist of any arguments given on the first line of the program,
|
---|
| 377 | followed by the name of the program, followed by the arguments passed
|
---|
| 378 | to the program.
|
---|
| 379 | .h "Redirection"
|
---|
| 380 | Input/output redirections can be intermixed with the words in a simple
|
---|
| 381 | command and can be placed following any of the other commands. When
|
---|
| 382 | redirection occurs, the shell saves the old values of the file descriptors
|
---|
| 383 | and restores them when the command completes. The ``<'', ``>'', and ``>>''
|
---|
| 384 | redirections open a file for input, output, and appending, respectively.
|
---|
| 385 | The ``<&digit'' and ``>&digit'' makes the input or output a duplicate
|
---|
| 386 | of the file descriptor numbered by the digit. If a minus sign is used
|
---|
| 387 | in place of a digit, the standard input or standard output are closed.
|
---|
| 388 | .PP
|
---|
| 389 | The ``<<\ word'' redirection
|
---|
| 390 | takes input from a
|
---|
| 391 | .I here
|
---|
| 392 | document.
|
---|
| 393 | As the shell encounters ``<<'' redirections, it collects them. The
|
---|
| 394 | next time it encounters an unescaped newline, it reads the documents
|
---|
| 395 | in turn. The word following the ``<<'' specifies the contents of the
|
---|
| 396 | line that terminates the document. If none of the quoting methods
|
---|
| 397 | ('', "", or \e) are used to enter the word, then the document is treated
|
---|
| 398 | like a word inside double quotes: ``$'' and backquote are expanded
|
---|
| 399 | and backslash can be used to escape these and to continue long lines.
|
---|
| 400 | The word cannot contain any variable or command substitutions, and
|
---|
| 401 | its length (after quoting) must be in the range of 1 to 79 characters.
|
---|
| 402 | If ``<<-'' is used in place of ``<<'', then leading tabs are deleted
|
---|
| 403 | from the lines of the document. (This is to allow you do indent shell
|
---|
| 404 | procedures containing here documents in a natural fashion.)
|
---|
| 405 | .PP
|
---|
| 406 | Any of the preceding redirection operators may be preceded by a single
|
---|
| 407 | digit specifying the file descriptor to be redirected. There cannot
|
---|
| 408 | be any white space between the digit and the redirection operator.
|
---|
| 409 | .h "Path Search"
|
---|
| 410 | When locating a command, the shell first looks to see if it has a
|
---|
| 411 | shell function by that name. Then, if PATH does not contain an
|
---|
| 412 | entry for "%builtin", it looks for a builtin command by that name.
|
---|
| 413 | Finally, it searches each entry in PATH in turn for the command.
|
---|
| 414 | .PP
|
---|
| 415 | The value of the PATH variable should be a series of entries separated
|
---|
| 416 | by colons.
|
---|
| 417 | Each entry consists of a directory name, or a directory name followed
|
---|
| 418 | by a flag beginning with a percent sign.
|
---|
| 419 | The current directory should be indicated by an empty directory name.
|
---|
| 420 | .PP
|
---|
| 421 | If no percent sign is present, then the entry causes the shell to
|
---|
| 422 | search for the command in the specified directory. If the flag is
|
---|
| 423 | ``%builtin'' then the list of shell builtin commands is searched.
|
---|
| 424 | If the flag is ``%func'' then the directory is searched for a file which
|
---|
| 425 | is read as input to the shell. This file should define a function
|
---|
| 426 | whose name is the name of the command being searched for.
|
---|
| 427 | .PP
|
---|
| 428 | Command names containing a slash are simply executed without performing
|
---|
| 429 | any of the above searches.
|
---|
| 430 | .h "The Environment"
|
---|
| 431 | The environment of a command is a set of name/value pairs. When the
|
---|
| 432 | shell is invoked, it reads these names and values, sets the shell
|
---|
| 433 | variables with these names to the corresponding values, and marks
|
---|
| 434 | the variables as exported. The
|
---|
| 435 | .I export
|
---|
| 436 | command can be used to mark additional variables as exported.
|
---|
| 437 | .PP
|
---|
| 438 | The environment of a command is constructed by constructing name/value
|
---|
| 439 | pairs from all the exported shell variables, and then modifying this
|
---|
| 440 | set by the assignments which precede the command, if any.
|
---|
| 441 | .h "Expansion"
|
---|
| 442 | The process of evaluating words when a shell procedure is executed is
|
---|
| 443 | called
|
---|
| 444 | .IR expansion .
|
---|
| 445 | Expansion consists of four steps: variable substitution, command
|
---|
| 446 | substitution, word splitting, and file name generation. If a word
|
---|
| 447 | is the expression following the word
|
---|
| 448 | .B case
|
---|
| 449 | in a case statement, the file name
|
---|
| 450 | which follows a redirection symbol, or an assignment to the environment
|
---|
| 451 | of a command, then the word cannot be split into multiple words. In
|
---|
| 452 | these cases, the last two steps of the expansion process are omitted.
|
---|
| 453 | .h "Variable Substitution"
|
---|
| 454 | To be written.
|
---|
| 455 | .h "Command Substitution"
|
---|
| 456 | .I Ash
|
---|
| 457 | accepts two syntaxes for command substitution:
|
---|
| 458 | .d
|
---|
| 459 | `\fIlist\fR`
|
---|
| 460 | .e
|
---|
| 461 | and
|
---|
| 462 | .d
|
---|
| 463 | $(\fIlist\fR)
|
---|
| 464 | .e
|
---|
| 465 | Either of these may be included in a word.
|
---|
| 466 | During the command substitution process, the command (syntactly a
|
---|
| 467 | .IR list )
|
---|
| 468 | will be executed and anything that the command writes to the standard
|
---|
| 469 | output will be captured by the shell. The final newline (if any) of
|
---|
| 470 | the output will be deleted; the rest of the output will be substituted
|
---|
| 471 | for the command in the word.
|
---|
| 472 | .h "Word Splitting"
|
---|
| 473 | When the value of a variable or the output of a command is substituted,
|
---|
| 474 | the resulting text is subject to word splitting, unless the dollar sign
|
---|
| 475 | introducing the variable or backquotes containing the text were enclosed
|
---|
| 476 | in double quotes. In addition, ``$@'' is subject to a special type of
|
---|
| 477 | splitting, even in the presence of double quotes.
|
---|
| 478 | .PP
|
---|
| 479 | Ash uses two different splitting algorithms. The normal approach, which
|
---|
| 480 | is intended for splitting text separated by which space, is used if the
|
---|
| 481 | first character of the shell variable IFS is a space. Otherwise an alternative
|
---|
| 482 | experimental algorithm, which is useful for splitting (possibly empty)
|
---|
| 483 | fields separated by a separator character, is used.
|
---|
| 484 | .PP
|
---|
| 485 | When performing splitting, the shell scans the replacement text looking
|
---|
| 486 | for a character (when IFS does not begin with a space) or a sequence of
|
---|
| 487 | characters (when IFS does begin with a space), deletes the character or
|
---|
| 488 | sequence of characters, and spits the word into two strings at that
|
---|
| 489 | point. When IFS begins with a space, the shell deletes either of the
|
---|
| 490 | strings if they are null. As a special case, if the word containing
|
---|
| 491 | the replacement text is the null string, the word is deleted.
|
---|
| 492 | .PP
|
---|
| 493 | The variable ``$@'' is special in two ways. First, splitting takes
|
---|
| 494 | place between the positional parameters, even if the text is enclosed
|
---|
| 495 | in double quotes. Second, if the word containing the replacement
|
---|
| 496 | text is the null string and there are no positional parameters, then
|
---|
| 497 | the word is deleted. The result of these rules is that "$@" is
|
---|
| 498 | equivalent to "$1" "$2" ... "$\fIn\fR", where \fIn\fR is the number of
|
---|
| 499 | positional parameters. (Note that this differs from the System V shell.
|
---|
| 500 | The System V documentation claims that "$@" behaves this way; in fact
|
---|
| 501 | on the System V shell "$@" is equivalent to "" when there are no
|
---|
| 502 | positional paramteters.)
|
---|
| 503 | .h "File Name Generation"
|
---|
| 504 | Unless the
|
---|
| 505 | .B -f
|
---|
| 506 | flag is set, file name generation is performed after word splitting is
|
---|
| 507 | complete. Each word is viewed as a series of patterns, separated by
|
---|
| 508 | slashes. The process of expansion replaces the word with the names of
|
---|
| 509 | all existing files whose names can be formed by replacing each pattern
|
---|
| 510 | with a string that matches the specified pattern. There are two
|
---|
| 511 | restrictions on this: first, a pattern cannot match a string containing
|
---|
| 512 | a slash, and second, a pattern cannot match a string starting with a
|
---|
| 513 | period unless the first character of the pattern is a period.
|
---|
| 514 | .PP
|
---|
| 515 | If a word fails to match any files and the
|
---|
| 516 | .B -z
|
---|
| 517 | flag is not set, then the word will be left unchanged (except that the
|
---|
| 518 | meta-characters will be converted to normal characters). If the
|
---|
| 519 | .B -z
|
---|
| 520 | flag is set, then the word is only left unchanged if none
|
---|
| 521 | of the patterns contain a character that can match anything besides
|
---|
| 522 | itself. Otherwise the
|
---|
| 523 | .B -z
|
---|
| 524 | flag forces the word to be replaced with the names of the files that it
|
---|
| 525 | matches, even if there are zero names.
|
---|
| 526 | .h "Patterns"
|
---|
| 527 | A
|
---|
| 528 | .I pattern
|
---|
| 529 | consists of normal characters, which match themselves, and meta-characters.
|
---|
| 530 | The meta-characters are ``!'', ``*'', ``?'', and ``[''. These characters lose
|
---|
| 531 | there special meanings if they are quoted. When command or variable
|
---|
| 532 | substitution is performed and the dollar sign or back quotes are not
|
---|
| 533 | double quoted, the value of the variable or the output of the command
|
---|
| 534 | is scanned for these characters and they are turned into meta-characters.
|
---|
| 535 | .PP
|
---|
| 536 | Two exclamation points at the beginning of a pattern function as a ``not''
|
---|
| 537 | operator, causing the pattern to match any string that the remainder of
|
---|
| 538 | the pattern does
|
---|
| 539 | .I not
|
---|
| 540 | match. Other occurances of exclamation points in a pattern match
|
---|
| 541 | exclamation points. Two exclamation points are required rather than one
|
---|
| 542 | to decrease the incompatibility with the System V shell (which does not
|
---|
| 543 | treat exclamation points specially).
|
---|
| 544 | .PP
|
---|
| 545 | An asterisk (``*'') matches any string of characters.
|
---|
| 546 | A question mark matches any single character.
|
---|
| 547 | A left bracket (``['') introduces a character class. The end of the
|
---|
| 548 | character class is indicated by a ``]''; if the ``]'' is missing then
|
---|
| 549 | the ``['' matches a ``['' rather than introducing a character class.
|
---|
| 550 | A character class matches any of the characters between the square
|
---|
| 551 | brackets. A range of characters may be specified using a minus sign.
|
---|
| 552 | The character class may be complemented by making an exclamation point
|
---|
| 553 | the first character of the character class.
|
---|
| 554 | .PP
|
---|
| 555 | To include a ``]'' in a character class, make it the first character listed
|
---|
| 556 | (after the ``!'', if any).
|
---|
| 557 | To include a minus sign, make it the first or last character listed.
|
---|
| 558 | .h "The /u Directory"
|
---|
| 559 | By convention, the name ``/u/user'' refers to the home directory of the
|
---|
| 560 | specified user. There are good reasons why this feature should be supported
|
---|
| 561 | by the file system (using a feature such as symbolic links) rather than
|
---|
| 562 | by the shell, but
|
---|
| 563 | .I ash
|
---|
| 564 | is capable of performing this mapping if the file system doesn't.
|
---|
| 565 | If the mapping is done by
|
---|
| 566 | .IR ash ,
|
---|
| 567 | setting the
|
---|
| 568 | .B -f
|
---|
| 569 | flag will turn it off.
|
---|
| 570 | .h "Character Set"
|
---|
| 571 | .I Ash
|
---|
| 572 | silently discards nul characters. Any other character will be handled
|
---|
| 573 | correctly by
|
---|
| 574 | .IR ash ,
|
---|
| 575 | including characters with the high order bit set.
|
---|
| 576 | .h "Job Names and Job Control"
|
---|
| 577 | The term
|
---|
| 578 | .I job
|
---|
| 579 | refers to a process created by a shell command, or in the case of a
|
---|
| 580 | pipeline, to the set of processes in the pipeline. The ways to refer
|
---|
| 581 | to a job are:
|
---|
| 582 | .d
|
---|
| 583 | %\fInumber\fR
|
---|
| 584 | %\fIstring\fR
|
---|
| 585 | %%
|
---|
| 586 | \fIprocess_id\fR
|
---|
| 587 | .e
|
---|
| 588 | The first form identifies a job by job number.
|
---|
| 589 | When a command is run,
|
---|
| 590 | .I ash
|
---|
| 591 | assigns it a job number
|
---|
| 592 | (the lowest unused number is assigned).
|
---|
| 593 | The second form identifies a job by giving a prefix of the command used
|
---|
| 594 | to create the job. The prefix must be unique. If there is only one job,
|
---|
| 595 | then the null prefix will identify the job, so you can refer to the job
|
---|
| 596 | by writing ``%''. The third form refers to the \fIcurrent job\fR. The
|
---|
| 597 | current job is the last job to be stopped while it was in the foreground.
|
---|
| 598 | (See the next paragraph.) The last form identifies a job by giving the
|
---|
| 599 | process id of the last process in the job.
|
---|
| 600 | .PP
|
---|
| 601 | If the operating system that
|
---|
| 602 | .I ash
|
---|
| 603 | is running on supports job control,
|
---|
| 604 | .I ash
|
---|
| 605 | will allow you to use it.
|
---|
| 606 | In this case, typing the suspend character (typically ^Z) while running
|
---|
| 607 | a command will return you to
|
---|
| 608 | .I ash
|
---|
| 609 | and will make the suspended command the current job. You can then continue
|
---|
| 610 | the job in the background by typing
|
---|
| 611 | .IR bg ,
|
---|
| 612 | or you can continue it in the foreground by typing
|
---|
| 613 | .IR fg .
|
---|
| 614 | .h "Atty"
|
---|
| 615 | If the shell variable ATTY is set, and the shell variable TERM is not
|
---|
| 616 | set to ``emacs'', then \fIash\fR generates appropriate escape sequences
|
---|
| 617 | to talk to
|
---|
| 618 | .IR atty (1).
|
---|
| 619 | .h "Exit Statuses"
|
---|
| 620 | By tradition, an exit status of zero means that a command has succeeded
|
---|
| 621 | and a nonzero exit status indicates that the command failed. This is
|
---|
| 622 | better than no convention at all, but in practice it is extremely useful
|
---|
| 623 | to allow commands that succeed to use the exit status to return information
|
---|
| 624 | to the caller. A variety of better conventions have been proposed, but
|
---|
| 625 | none of them has met with universal approval. The convention used by
|
---|
| 626 | \fIash\fR and all the programs included in the \fIash\fR distribution is
|
---|
| 627 | as follows:
|
---|
| 628 | .ta 1i 2i
|
---|
| 629 | .nf
|
---|
| 630 | 0 Success.
|
---|
| 631 | 1 Alternate success.
|
---|
| 632 | 2 Failure.
|
---|
| 633 | 129-... Command terminated by a signal.
|
---|
| 634 | .fi
|
---|
| 635 | The \fIalternate success\fR return is used by commands to indicate various
|
---|
| 636 | conditions which are not errors but which can, with a little imagination,
|
---|
| 637 | be conceived of as less successful than plain success. For example,
|
---|
| 638 | .I test
|
---|
| 639 | returns 1 when the tested condition is false and
|
---|
| 640 | .I getopts
|
---|
| 641 | returns 1 when there are no more options.
|
---|
| 642 | Because this convention is not used universally, the
|
---|
| 643 | .B -e
|
---|
| 644 | option of
|
---|
| 645 | .I ash
|
---|
| 646 | causes the shell to exit when a command returns 1 even though that
|
---|
| 647 | contradicts the convention described here.
|
---|
| 648 | .PP
|
---|
| 649 | When a command is terminated by a signal, the uses 128 plus the signal
|
---|
| 650 | number as the exit code for the command.
|
---|
| 651 | .h "Builtin Commands"
|
---|
| 652 | This concluding section lists the builtin commands which are builtin
|
---|
| 653 | because they need to perform some operation that can't be performed by a
|
---|
| 654 | separate process. In addition to these, there are several other commands
|
---|
| 655 | .RI ( catf ,
|
---|
| 656 | .IR echo ,
|
---|
| 657 | .IR expr ,
|
---|
| 658 | .IR line ,
|
---|
| 659 | .IR nlecho ,
|
---|
| 660 | .IR test ,
|
---|
| 661 | .RI `` : '',
|
---|
| 662 | and
|
---|
| 663 | .IR true )
|
---|
| 664 | which can optionally be compiled into the shell. The builtin
|
---|
| 665 | commands described below that accept options use the System V Release 2
|
---|
| 666 | .IR getopt (3)
|
---|
| 667 | syntax.
|
---|
| 668 | .sp
|
---|
| 669 | .b bg
|
---|
| 670 | [
|
---|
| 671 | .I job
|
---|
| 672 | ] ...
|
---|
| 673 | .br
|
---|
| 674 | Continue the specified jobs (or the current job if no jobs are given)
|
---|
| 675 | in the background.
|
---|
| 676 | This command is only available on systems with Bekeley job control.
|
---|
| 677 | .b command
|
---|
| 678 | .IR "command arg" ...
|
---|
| 679 | .br
|
---|
| 680 | Execute the specified builtin command. (This is useful when you have a
|
---|
| 681 | shell function with the same name as a builtin command.)
|
---|
| 682 | .b cd
|
---|
| 683 | [
|
---|
| 684 | .I directory
|
---|
| 685 | ]
|
---|
| 686 | .br
|
---|
| 687 | Switch to the specified directory (default $HOME).
|
---|
| 688 | If the an entry for CDPATH appears in the environment of the cd command
|
---|
| 689 | or the shell variable CDPATH is set and the directory name does not
|
---|
| 690 | begin with a slash, then the directories listed in CDPATH will be
|
---|
| 691 | searched for the specified directory. The format of CDPATH is the
|
---|
| 692 | same as that of PATH.
|
---|
| 693 | In an interactive shell, the cd command will print out the name of the
|
---|
| 694 | directory that it actually switched to if this is different from the
|
---|
| 695 | name that the user gave. These may be different either because
|
---|
| 696 | the CDPATH mechanism was used or because a symbolic link was crossed.
|
---|
| 697 | .\" .b ".\fI\h'0.1i'file"
|
---|
| 698 | .\" Cawf can't do \h'0.1i'
|
---|
| 699 | .b .
|
---|
| 700 | .I file
|
---|
| 701 | .br
|
---|
| 702 | The commands in the specified file are read and executed by the shell.
|
---|
| 703 | A path search is not done to find the file because the directories in
|
---|
| 704 | PATH generally contain files that are intended to be executed, not read.
|
---|
| 705 | .b eval
|
---|
| 706 | .IR string ...
|
---|
| 707 | .br
|
---|
| 708 | The strings are parsed as shell commands and executed.
|
---|
| 709 | (This differs from the System V shell, which concatenates the arguments
|
---|
| 710 | (separated by spaces) and parses the result as a single command.)
|
---|
| 711 | .b exec
|
---|
| 712 | [
|
---|
| 713 | .IR "command arg" ...
|
---|
| 714 | ]
|
---|
| 715 | .br
|
---|
| 716 | Unless
|
---|
| 717 | .I command
|
---|
| 718 | is omitted,
|
---|
| 719 | the shell process is replaced with the specified program (which must be a real
|
---|
| 720 | program, not a shell builtin or function).
|
---|
| 721 | Any redirections on the exec command are marked as permanent, so that they
|
---|
| 722 | are not undone when the exec command finishes.
|
---|
| 723 | If the command is not found, the exec command causes the shell to exit.
|
---|
| 724 | .b exit
|
---|
| 725 | [
|
---|
| 726 | .I exitstatus
|
---|
| 727 | ]
|
---|
| 728 | .br
|
---|
| 729 | Terminate the shell process. If
|
---|
| 730 | .I exitstatus
|
---|
| 731 | is given it is used as the
|
---|
| 732 | exit status of the shell; otherwise the exit status of the preceding
|
---|
| 733 | command is used.
|
---|
| 734 | .b export
|
---|
| 735 | .IR name ...
|
---|
| 736 | .br
|
---|
| 737 | The specified names are exported so that they will appear in the environment
|
---|
| 738 | of subsequent commands. The only way to un-export a variable is to unset it.
|
---|
| 739 | .I Ash
|
---|
| 740 | allows the value of a variable to be set at the same time it is exported
|
---|
| 741 | by writing
|
---|
| 742 | .d
|
---|
| 743 | \fBexport\fR name=value
|
---|
| 744 | .e
|
---|
| 745 | With no arguments the export command lists the names of all exported variables.
|
---|
| 746 | .b fg
|
---|
| 747 | [
|
---|
| 748 | .I job
|
---|
| 749 | ]
|
---|
| 750 | .br
|
---|
| 751 | Move the specified job or the current job to the foreground.
|
---|
| 752 | This command is only available on systems with Bekeley job control.
|
---|
| 753 | .b getopts
|
---|
| 754 | .I optstring
|
---|
| 755 | .I var
|
---|
| 756 | .br
|
---|
| 757 | The System V
|
---|
| 758 | .I getopts
|
---|
| 759 | command.
|
---|
| 760 | .b hash
|
---|
| 761 | .B -rv
|
---|
| 762 | .IR command ...
|
---|
| 763 | .br
|
---|
| 764 | The shell maintains a hash table which remembers the locations of
|
---|
| 765 | commands. With no arguments whatsoever, the hash command prints
|
---|
| 766 | out the contents of this table. Entries which have not been looked
|
---|
| 767 | at since the last
|
---|
| 768 | .I cd
|
---|
| 769 | command are marked with an asterisk; it is possible for these entries
|
---|
| 770 | to be invalid.
|
---|
| 771 | .sp
|
---|
| 772 | With arguments, the hash command removes the specified commands from
|
---|
| 773 | the hash table (unless they are functions) and then locates them.
|
---|
| 774 | With the
|
---|
| 775 | .B -v
|
---|
| 776 | option,
|
---|
| 777 | .I hash
|
---|
| 778 | prints the locations of the commands as it finds them.
|
---|
| 779 | The
|
---|
| 780 | .B -r
|
---|
| 781 | option causes the
|
---|
| 782 | .I hash
|
---|
| 783 | command to delete all the entries in the hash table except for
|
---|
| 784 | functions.
|
---|
| 785 | .b jobid
|
---|
| 786 | [
|
---|
| 787 | .I job
|
---|
| 788 | ]
|
---|
| 789 | .br
|
---|
| 790 | Print the process id's of the processes in the job. If the job argument
|
---|
| 791 | is omitted, use the current job.
|
---|
| 792 | .b jobs
|
---|
| 793 | .br
|
---|
| 794 | This command lists out all the background processes which are children
|
---|
| 795 | of the current shell process.
|
---|
| 796 | .b pwd
|
---|
| 797 | .br
|
---|
| 798 | Print the current directory. The builtin command may differ from the
|
---|
| 799 | program of the same name because the builtin command remembers what
|
---|
| 800 | the current directory is rather than recomputing it each time. This
|
---|
| 801 | makes it faster. However, if the current directory is renamed, the
|
---|
| 802 | builtin version of pwd will continue to print the old name for the
|
---|
| 803 | directory.
|
---|
| 804 | .b read
|
---|
| 805 | [
|
---|
| 806 | .B -p
|
---|
| 807 | .I prompt
|
---|
| 808 | ]
|
---|
| 809 | [
|
---|
| 810 | .B -e
|
---|
| 811 | ]
|
---|
| 812 | .IR variable ...
|
---|
| 813 | .br
|
---|
| 814 | The prompt is printed if the
|
---|
| 815 | .B -p
|
---|
| 816 | option is specified and the standard input is a terminal. Then a
|
---|
| 817 | line is read from the standard input. The trailing newline is deleted
|
---|
| 818 | from the line and the line is split as described
|
---|
| 819 | in the section on word splitting above, and the pieces are assigned to
|
---|
| 820 | the variables in order. If there are more pieces than variables, the
|
---|
| 821 | remaining pieces (along with the characters in IFS that separated them)
|
---|
| 822 | are assigned to the last variable. If there are more variables than
|
---|
| 823 | pieces, the remaining variables are assigned the null string.
|
---|
| 824 | .sp
|
---|
| 825 | The
|
---|
| 826 | .B -e
|
---|
| 827 | option causes any backslashes in the input to be treated specially.
|
---|
| 828 | If a backslash is followed by a newline, the backslash and the newline
|
---|
| 829 | will be deleted. If a backslash is followed by any other character,
|
---|
| 830 | the backslash will be deleted and the following character will be treated
|
---|
| 831 | as though it were not in IFS, even if it is.
|
---|
| 832 | .b readonly
|
---|
| 833 | .IR name ...
|
---|
| 834 | .br
|
---|
| 835 | The specified names are marked as read only, so that they cannot be
|
---|
| 836 | subsequently modified or unset.
|
---|
| 837 | .I Ash
|
---|
| 838 | allows the value of a variable to be set at the same time it is marked
|
---|
| 839 | read only by writing
|
---|
| 840 | .d
|
---|
| 841 | \fBreadonly\fR name=value
|
---|
| 842 | .e
|
---|
| 843 | With no arguments the readonly command lists the names of all
|
---|
| 844 | read only variables.
|
---|
| 845 | .b set
|
---|
| 846 | [
|
---|
| 847 | {
|
---|
| 848 | .BI - options
|
---|
| 849 | |
|
---|
| 850 | .BI + options
|
---|
| 851 | |
|
---|
| 852 | .B --
|
---|
| 853 | }
|
---|
| 854 | ]
|
---|
| 855 | .IR arg ...
|
---|
| 856 | .br
|
---|
| 857 | The
|
---|
| 858 | .I set
|
---|
| 859 | command performs three different functions.
|
---|
| 860 | .sp
|
---|
| 861 | With no arguments, it lists the values of all shell variables.
|
---|
| 862 | .sp
|
---|
| 863 | If options are given, it sets the specified option flags, or clears
|
---|
| 864 | them if the option flags are introduced with a
|
---|
| 865 | .B +
|
---|
| 866 | rather than a
|
---|
| 867 | .BR - .
|
---|
| 868 | Only the first argument to
|
---|
| 869 | .I set
|
---|
| 870 | can contain options.
|
---|
| 871 | The possible options are:
|
---|
| 872 | .sp
|
---|
| 873 | .ta 0.4i
|
---|
| 874 | .in +0.4i
|
---|
| 875 | .ti -0.4i
|
---|
| 876 | \fB-e\fR Causes the shell to exit when a command terminates with
|
---|
| 877 | a nonzero exit status, except when the exit status of the command is
|
---|
| 878 | explicitly tested. The exit status of a command is considered to be
|
---|
| 879 | explicitly tested if the command is used to control an
|
---|
| 880 | .IR if ,
|
---|
| 881 | .IR elif ,
|
---|
| 882 | .IR while ,
|
---|
| 883 | or
|
---|
| 884 | .IR until ;
|
---|
| 885 | or if the command is the left hand operand of an ``&&'' or ``||''
|
---|
| 886 | operator.
|
---|
| 887 | .sp
|
---|
| 888 | .ti -0.4i
|
---|
| 889 | \fB-f\fR Turn off file name generation.
|
---|
| 890 | .sp
|
---|
| 891 | .ti -0.4i
|
---|
| 892 | \fB-I\fR Cause the shell to ignore end of file conditions.
|
---|
| 893 | (This doesn't apply when the shell a script sourced using the ``.''
|
---|
| 894 | command.) The shell will in fact exit if it gets 50 eof's in a
|
---|
| 895 | row.
|
---|
| 896 | .sp
|
---|
| 897 | .ti -0.4i
|
---|
| 898 | \fB-i\fR Make the shell interactive. This causes the shell to
|
---|
| 899 | prompt for input, to trap interrupts, to ignore quit and terminate signals,
|
---|
| 900 | and to return to the main command loop rather than exiting on error.
|
---|
| 901 | .sp
|
---|
| 902 | .ti -0.4i
|
---|
| 903 | \fB-j\fR Turns on Berkeley job control, on systems that support it.
|
---|
| 904 | When the shell starts up, the
|
---|
| 905 | .B -j
|
---|
| 906 | is set by default if the
|
---|
| 907 | .B -i
|
---|
| 908 | flag is set.
|
---|
| 909 | .sp
|
---|
| 910 | .ti -0.4i
|
---|
| 911 | \fB-n\fR Causes the shell to read commands but not execute them.
|
---|
| 912 | (This is marginally useful for checking the syntax of scripts.)
|
---|
| 913 | .sp
|
---|
| 914 | .ti -0.4i
|
---|
| 915 | \fB-s\fR If this flag is set when the shell starts up, the shell
|
---|
| 916 | reads commands from its standard input. The shell doesn't examine the
|
---|
| 917 | value of this flag any other time.
|
---|
| 918 | .sp
|
---|
| 919 | .ti -0.4i
|
---|
| 920 | \fB-x\fR If this flag is set, the shell will print out each
|
---|
| 921 | command before executing it.
|
---|
| 922 | .sp
|
---|
| 923 | .ti -0.4i
|
---|
| 924 | \fB-z\fR If this flag is set, the file name generation process
|
---|
| 925 | may generate zero files. If it is not set, then a pattern which does
|
---|
| 926 | not match any files will be replaced by a quoted version of the pattern.
|
---|
| 927 | .in -0.4i
|
---|
| 928 | .sp
|
---|
| 929 | The third use of the set command is to set the values of the shell's
|
---|
| 930 | positional parameters to the specified
|
---|
| 931 | .IR args .
|
---|
| 932 | To change the positional parameters without changing any options,
|
---|
| 933 | use ``\fB--\fR'' as the first argument to
|
---|
| 934 | .IR set .
|
---|
| 935 | If no args are present, the set command will leave the value of the
|
---|
| 936 | positional parameters unchanged, so to set the positional parameters
|
---|
| 937 | to set of values that may be empty, execute the command
|
---|
| 938 | .d
|
---|
| 939 | shift $#
|
---|
| 940 | .e
|
---|
| 941 | first to clear out the old values of the positional parameters.
|
---|
| 942 | .b setvar
|
---|
| 943 | .I variable
|
---|
| 944 | .I value
|
---|
| 945 | .br
|
---|
| 946 | Assigns
|
---|
| 947 | .I value
|
---|
| 948 | to
|
---|
| 949 | .IR variable .
|
---|
| 950 | (In general it is better to write
|
---|
| 951 | .I variable=value
|
---|
| 952 | rather than using
|
---|
| 953 | .IR setvar .
|
---|
| 954 | .I Setvar
|
---|
| 955 | is intended to be used in functions that assign values to variables whose
|
---|
| 956 | names are passed as parameters.)
|
---|
| 957 | .b shift
|
---|
| 958 | [
|
---|
| 959 | .I n
|
---|
| 960 | ]
|
---|
| 961 | .br
|
---|
| 962 | Shift the positional parameters
|
---|
| 963 | .I n
|
---|
| 964 | times.
|
---|
| 965 | A shift sets the value of $1 to the value of $2, the value of $2 to
|
---|
| 966 | the value of $3, and so on, decreasing the value of $# by one.
|
---|
| 967 | If there are zero positional parameters, shifting doesn't do anything.
|
---|
| 968 | .b trap
|
---|
| 969 | [
|
---|
| 970 | .I action
|
---|
| 971 | ]
|
---|
| 972 | .IR signal ...
|
---|
| 973 | .br
|
---|
| 974 | Cause the shell to parse and execute
|
---|
| 975 | .I action
|
---|
| 976 | when any of the specified signals are received.
|
---|
| 977 | The signals are specified by signal number.
|
---|
| 978 | .I Action
|
---|
| 979 | may be null or omitted;
|
---|
| 980 | the former causes the specified signal to be ignored and the latter
|
---|
| 981 | causes the default action to be taken.
|
---|
| 982 | When the shell forks off a subshell, it resets trapped (but not ignored)
|
---|
| 983 | signals to the default action.
|
---|
| 984 | The trap command has no effect on signals that were ignored on entry
|
---|
| 985 | to the shell.
|
---|
| 986 | .b umask
|
---|
| 987 | [
|
---|
| 988 | .I mask
|
---|
| 989 | ]
|
---|
| 990 | .br
|
---|
| 991 | Set the value of umask (see
|
---|
| 992 | .IR umask (2))
|
---|
| 993 | to the specified octal value. If the argument is omitted, the umask
|
---|
| 994 | value is printed.
|
---|
| 995 | .b unset
|
---|
| 996 | .IR name ...
|
---|
| 997 | .br
|
---|
| 998 | The specified variables and functions are unset and unexported.
|
---|
| 999 | If a given name corresponds to both a variable and a function, both the
|
---|
| 1000 | variable and the function are unset.
|
---|
| 1001 | .b wait
|
---|
| 1002 | [
|
---|
| 1003 | .I job
|
---|
| 1004 | ]
|
---|
| 1005 | .br
|
---|
| 1006 | Wait for the specified job to complete and return the exit status of the
|
---|
| 1007 | last process in the job. If the argument is omitted, wait for all jobs
|
---|
| 1008 | to complete and the return an exit status of zero.
|
---|
| 1009 | .SH EXAMPLES
|
---|
| 1010 | The following function redefines the \fIcd\fR command:
|
---|
| 1011 | .d
|
---|
| 1012 | cd() {
|
---|
| 1013 | if command cd "$@"
|
---|
| 1014 | then if test -f .enter
|
---|
| 1015 | then . .enter
|
---|
| 1016 | else return 0
|
---|
| 1017 | fi
|
---|
| 1018 | fi
|
---|
| 1019 | }
|
---|
| 1020 | .e
|
---|
| 1021 | This function causes the file ``.enter'' to be read when you enter a
|
---|
| 1022 | directory, if it exists. The \fIcommand\fR command is used to access the
|
---|
| 1023 | real \fIcd\fR command. The ``return 0'' ensures that the function will
|
---|
| 1024 | return an exit status of zero if it successfully changes to a directory
|
---|
| 1025 | that does not contain a ``.enter'' file. Redefining existing commands
|
---|
| 1026 | is not always a good idea, but this example shows that you can do it if
|
---|
| 1027 | you want to.
|
---|
| 1028 | .PP
|
---|
| 1029 | The suspend function distributed with
|
---|
| 1030 | .I ash
|
---|
| 1031 | looks like
|
---|
| 1032 | .d
|
---|
| 1033 | # Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
|
---|
| 1034 | # This file is part of ash, which is distributed under the terms
|
---|
| 1035 | # specified by the Ash General Public License.
|
---|
| 1036 |
|
---|
| 1037 | suspend() {
|
---|
| 1038 | local -
|
---|
| 1039 | set +j
|
---|
| 1040 | kill -TSTP 0
|
---|
| 1041 | }
|
---|
| 1042 | .e
|
---|
| 1043 | This turns off job control and then sends a stop signal to the current
|
---|
| 1044 | process group, which suspends the shell. (When job control is turned
|
---|
| 1045 | on, the shell ignores the TSTP signal.) Job control will be turned back
|
---|
| 1046 | on when the function returns because ``-'' is local to the function.
|
---|
| 1047 | As an example of what \fInot\fR to do, consider an earlier version of
|
---|
| 1048 | \fIsuspend\fR:
|
---|
| 1049 | .d
|
---|
| 1050 | suspend() {
|
---|
| 1051 | suspend_flag=$-
|
---|
| 1052 | set +j
|
---|
| 1053 | kill -TSTP 0
|
---|
| 1054 | set -$suspend_flag
|
---|
| 1055 | }
|
---|
| 1056 | .e
|
---|
| 1057 | There are two problems with this. First, \fBsuspend_flag\fR is a global
|
---|
| 1058 | variable rather than a local one, which will cause problems in the
|
---|
| 1059 | (unlikely) circumstance that the user is using that variable for some
|
---|
| 1060 | other purpose. Second, consider what happens if shell received an interrupt
|
---|
| 1061 | signal after it executes the first \fIset\fR command but before it executes
|
---|
| 1062 | the second one. The interrupt signal will abort the shell function, so
|
---|
| 1063 | that the second \fIset\fR command will never be executed and job control
|
---|
| 1064 | will be left off. The first version of \fIsuspend\fR avoids this problem
|
---|
| 1065 | by turning job control off only in a local copy of the shell options. The
|
---|
| 1066 | local copy of the shell options is discarded when the function is terminated,
|
---|
| 1067 | no matter how it is terminated.
|
---|
| 1068 | .SH HINTS
|
---|
| 1069 | Shell variables can be used to provide abbreviations for things which
|
---|
| 1070 | you type frequently. For example, I set
|
---|
| 1071 | .br
|
---|
| 1072 | .\" \h'1i'export h=$HOME
|
---|
| 1073 | .\" Cawf can't do \h'1i'
|
---|
| 1074 | .in +1i
|
---|
| 1075 | export h=$HOME
|
---|
| 1076 | .in -1i
|
---|
| 1077 | .br
|
---|
| 1078 | in my .profile so that I can type the name of my home directory simply
|
---|
| 1079 | by typing ``$h''.
|
---|
| 1080 | .PP
|
---|
| 1081 | When writing shell procedures, try not to make assumptions about what is
|
---|
| 1082 | imported from the environment. Explicitly unset or initialize all variables,
|
---|
| 1083 | rather than assuming they will be unset. If you use cd, it is a good idea
|
---|
| 1084 | to unset CDPATH.
|
---|
| 1085 | .PP
|
---|
| 1086 | People sometimes use ``<&-'' or ``>&-'' to provide no input to a command
|
---|
| 1087 | or to discard the output of a command. A better way to do this is
|
---|
| 1088 | to redirect the input or output of the command to
|
---|
| 1089 | .BR /dev/null .
|
---|
| 1090 | .PP
|
---|
| 1091 | Word splitting and file name generation are performed by default,
|
---|
| 1092 | and you have to explicitly use double quotes to suppress it. This is
|
---|
| 1093 | backwards, but you can learn to live with it. Just get in the habit of
|
---|
| 1094 | writing double quotes around variable and command substitutions, and
|
---|
| 1095 | omit them only when you really want word splitting and file name generation.
|
---|
| 1096 | If you want word splitting but not file name generation, use the
|
---|
| 1097 | .B -f
|
---|
| 1098 | option.
|
---|
| 1099 | .SH AUTHORS
|
---|
| 1100 | Kenneth Almquist
|
---|
| 1101 | .SH "SEE ALSO"
|
---|
| 1102 | echo(1), expr(1), line(1), pwd(1), true(1).
|
---|
| 1103 | .SH BUGS
|
---|
| 1104 | When command substitution occurs inside a here document, the commands inside
|
---|
| 1105 | the here document are run with their standard input closed. For example,
|
---|
| 1106 | the following will not work because the standard input of the
|
---|
| 1107 | .I line
|
---|
| 1108 | command will be closed when the command is run:
|
---|
| 1109 | .d
|
---|
| 1110 | cat <<-!
|
---|
| 1111 | Line 1: $(line)
|
---|
| 1112 | Line 2: $(line)
|
---|
| 1113 | !
|
---|
| 1114 | .e
|
---|
| 1115 | .PP
|
---|
| 1116 | Unsetting a function which is currently being executed may cause strange
|
---|
| 1117 | behavior.
|
---|
| 1118 | .PP
|
---|
| 1119 | The shell syntax allows a here document to be terminated by an end of file
|
---|
| 1120 | as well as by a line containing the terminator word which follows the ``<<''.
|
---|
| 1121 | What this means is that if you mistype the terminator line, the shell
|
---|
| 1122 | will silently swallow up the rest of your shell script and stick it
|
---|
| 1123 | in the here document.
|
---|
| 1124 | .\" several minor typos corrected -- ASW 2005-01-15
|
---|