Home | History | Annotate | Download | only in common
      1 .	 \" use troff -mm
      2 .nr C 3
      3 .nr N 2 
      4 .SA 1  \"  right justified
      5 .ND "December 21, 1993"
      6 .TL "311466-6713" "61175"  \" charging case filing case 
      7 Introduction to \f5ksh-93\fP
      8 .AU "David G. Korn" DGK MH 11267 7975 3C-526B "(research!dgk)"
      9 .TM  11267-931221-26  \"  technical memo + TM numbers
     10 .MT 1  \"  memo type
     11 .OK Shell "Command interpreter" Language UNIX  \" keyword
     12 .AS 2   \" abstract start for TM 
     13 \f5ksh-93\fP is a
     14 major rewrite of \f5ksh\fP,
     15 a program that serves as a command language
     16 (shell) for the UNIX* 
     17 .FS  *
     18 UNIX is a registered trademark of Novell.
     19 .FE
     20 operating system.
     21 As with \f5ksh\fP, \f5ksh-93\fP
     22 is essentially compatible with the System V version of the Bourne shell\*(Rf,
     23 .RS
     24 S. R. Bourne,
     25 .I "An Introduction to the UNIX
     26 Shell,"
     27 BSTJ - Vol. 57, No. 6 part 2, pages 1947-1972, 1978.
     28 .RF
     29 and compatible with previous versions of \f5ksh\fP.
     30 \f5ksh-93\fP is intended to comply with the IEEE POSIX 1003.2
     31 and ISO 9945-2\*(Rf
     32 .RS
     33 .I "POSIX \- Part 2: Shell and Utilities,"
     34 IEEE Std 1003.2-1992, ISO/IEC 9945-2, IEEE, 1993.
     35 .RF
     36 shell standard.
     37 In addition to changes in the language required
     38 by these standards, the primary focus of \f5ksh-93\fP
     39 is related to shell programming.
     40 \f5ksh-93\fP provides the programming power of several
     41 other interpretive languages such as \f5awk\fP\*(Rf,
     42 .RS
     43 Al Aho,
     44 Brian Kernighan,
     45 and
     46 Peter Weinberger,
     47 .I "The AWK Programming Language,"
     48 Addison Wesley, 1988.
     49 .RF
     50 \f5FIT\fP\*(Rf,
     51 .RS
     52 Lloyd H. Nakatani and Laurence W. Ruedisueli,
     53 .I "The FIT Programming Language Primer",
     54 TM 1126-920301-03, 1992.
     55 .RF
     56 \f5PERL\fP\*(Rf,
     57 .RS
     58 Larry Wall and Randal Schwartz,
     59 .I "Programming perl,"
     60 O'Reilly & Assoc, 1990.
     61 .RF
     62 and
     63 \f5tcl\fP\*(Rf.
     64 .RS
     65 John K. Ousterhout,
     66 .I "Tcl: An Embeddable Command Language",
     67 Proceedings of the Washington USENIX meeting, pp. 133-146, 1990.
     68 .RF
     69 .P
     70 This memo
     71 assumes that the reader is already familiar with the Bourne shell.
     72 It introduces most of the features of \f5ksh-93\fP
     73 relative to the Bourne shell; both
     74 as a command language and as a programming language.
     75 The Appendix contains
     76 a sample script written in \f5ksh-93\fP.
     77 .AE   \" abstract end
     78 .H 1 "INTRODUCTION"
     79 .P
     80 The term "shell" is used to describe a program that provides
     81 a command language
     82 interface.
     83 Because the UNIX*\ 
     84 .FS *
     85 UNIX is a registered trademark of USL
     86 .FE
     87 system shell is a user level program, and not part of
     88 the operating system itself,
     89 anyone can write a new shell or modify an existing one.
     90 This has caused an evolutionary progress
     91 in the design and implementation of shells,
     92 with the better ones surviving.
     93 The most widely available UNIX system shells are the Bourne shell\*(Rf,
     94 .RS
     95 S. R. Bourne,
     96 .IR "An Introduction to the UNIX Shell" ,
     97 Bell System Technical Journal,
     98 Vol. 57, No. 6, Part 2, pp. 1947-1972, July 1978.
     99 .RF
    100 written by Steve Bourne
    101 at AT&T Bell Laboratories,
    102 the C shell\*(Rf,
    103 .RS
    104 W. Joy,
    105 .IR "An Introduction to the C Shell" ,
    106 Unix Programmer's Manual, Berkeley Software Distribution,
    107 University of California, Berkeley, 1980.
    108 .RF
    109 written by Bill Joy at the University of California, Berkeley,
    110 and the KornShell language \*(Rf,
    111 .RS
    112 Morris Bolsky and David Korn,
    113 .IR "The KornShell Command and Programming Language" ,
    114 Prentice Hall, 1989.
    115 .RF
    116 written by David Korn
    117 at AT&T Bell Laboratories.
    118 The Bourne shell is available on almost all versions of the UNIX
    119 system.
    120 The C Shell is available with all Berkeley Software Distribution (BSD) UNIX systems and on many other systems.
    121 The KornShell
    122 is available on System V Release 4 systems. 
    123 In addition, it is available on many other systems.
    124 The source for the KornShell language is available from the AT&T Toolchest,
    125 an electronic software distribution system.
    126 It runs on all known versions of the UNIX system and
    127 on many UNIX system look-alikes.
    128 .P
    129 There have been several articles comparing the UNIX system shells.
    130 Jason Levitt\*(Rf 
    131 .RS
    132 Jason Levitt,
    133 .IR "The Korn Shell: An Emerging Standard" ,
    134 UNIX/World, pp. 74-81, September 1986. 
    135 .RF
    136 highlights some of the new features
    137 introduced by the KornShell language.
    138 Rich Bilancia\*(Rf 
    139 .RS
    140 Rich Bilancia,
    141 .IR "Proficiency and Power are Yours With the Korn Shell" ,
    142 UNIX/World, pp. 103-107, September 1987. 
    143 .RF
    144 explains some of the advantages of using the KornShell language.
    145 John Sebes\*(Rf 
    146 .RS
    147 John Sebes,
    148 .I "Comparing UNIX Shells,"
    149 UNIX Papers,
    150 Edited by the Waite Group, Howard W. Sams & Co., 1987.
    151 .RF
    152 provides a more detailed comparison of the three shells,
    153 both as a command language and as a programming language.
    154 .P
    155 The KornShell language is a superset of the
    156 Bourne shell. The KornShell language has many of the popular C shell features,
    157 plus additional features of its own.
    158 Its initial popularity stems primarily from its improvements as
    159 a command language.
    160 The primary interactive benefit of the KornShell command language
    161 is a visual command line editor that allows you to
    162 make corrections to your current command line
    163 or to earlier command lines,
    164 without having to retype them.
    165 .P
    166 However,
    167 in the long run,
    168 the power of the KornShell language as a high-level programming language,
    169 as described by Dolotta and Mashey\*(Rf,
    170 .RS
    171 T. A. Dolotta and J. R. Mashey,
    172 .I "Using the shell as a Primary Programming Tool,"
    173 Proc. 2nd. Int. Conf. on Software Engineering, 1976,
    174 pages 169-176.
    175 .RF
    176 may prove to be of greater significance.
    177 \f5ksh-93\fP provides the programming power of several
    178 other interpretive languages such as \f5awk\fP,
    179 \f5FIT\fP,
    180 \f5PERL\fP,
    181 and
    182 \f5tcl\fP.
    183 An application that was originally written in the C programming language
    184 was rewritten in the KornShell language.
    185 More than 20,000 lines of C code were replaced with KornShell scripts
    186 totaling fewer than 700 lines.
    187 In most instances there was no perceptible difference in performance
    188 between the two versions of the code.
    189 .P
    190 The KornShell language has been embedded into windowing systems
    191 allowing graphical user interfaces to be developed in shell
    192 rather than having to build applications that need to be
    193 compiled.
    194 The \f5wksh\fP program\*(Rf
    195 .RS
    196 J. S. Pendergrast,
    197 .IR "WKSH - Korn Shell with X-Windows Support",
    198 USL. 1991.
    199 .RF
    200 provides a method of developing OpenLook or Motif
    201 applications as \f5ksh\fP scripts.
    202 .P
    203 This memo is an introduction to \f5ksh-93\fP,
    204 the program that implements an enhanced version
    205 of the KornShell language.
    206 It is referred to as \f5ksh\fP in the rest of this memo.
    207 The memo describes the KornShell language based on the
    208 features of the 12/28/93 release of \f5ksh\fP.
    209 This memo is not a tutorial, only an introduction.
    210 The second edition of reference [9] gives 
    211 a more complete treatment of the KornShell language.
    212 .P
    213 A concerted effort has been made to achieve both System V Bourne shell
    214 compatibility and IEEE POSIX compatibility
    215 so that scripts written for either of these shells
    216 can run without modification with \f5ksh\fP.
    217 In addition, \f5ksh-93\fP attempts to
    218 be compatible with older versions of \f5ksh\fP.
    219 When there are conflicts between versions of the shell,
    220 \f5ksh-93\fP selects the behavior dictated by the IEEE POSIX
    221 standard.
    222 The description of features in this memo assumes
    223 that the reader is already familiar with the Bourne shell.
    224 .H 1 "COMMAND LANGUAGE"
    225 There is no separate command language.
    226 All features of the language, except job control,
    227 can be
    228 used both within a script and interactively from a terminal.
    229 However, features that are more likely to be used
    230 while running commands interactively from a terminal
    231 are presented here.
    232 .H 2 "Setting Options"
    233 By convention, UNIX commands
    234 consist of a command name followed by options and other arguments.
    235 Options are either of the form \f5-\fP\fIletter\fP,
    236 or \f5-\fP\fIletter value\fP.
    237 In the former case, several options may be grouped after a single \f5-\fP.
    238 The argument \f5--\fP signifies an end to the option list and is
    239 only required when the first non-option argument begins with
    240 a \f5-\fP.
    241 Most commands print an error message which
    242 shows which options are permitted
    243 when given incorrect arguments.
    244 In addition, the option sequence \f5-?\fP causes most commands
    245 to print a usage message which lists the valid options. 
    246 .P
    247 Ordinarily, \f5ksh\fP executes a command by 
    248 using the command name to locate a program to run
    249 and by running the program as a separate process.
    250 Some commands, referred to as
    251 .IR built-ins ,
    252 are carried out by \f5ksh\fP itself,
    253 without creating a separate process.
    254 The reasons that some commands are built-in are presented later.
    255 In nearly all cases the distinction
    256 between a command that is built-in and one that
    257 is not is invisible to the user.
    258 However, nearly
    259 all commands that are built-in follow command line conventions.
    260 .P
    261 \f5ksh\fP has several options that can be set by the user
    262 as command line arguments at invocation and as option arguments to the
    263 \f5set\fP command.
    264 Most other options can be set with a single letter option or as a name
    265 that follows the \f5-o\fP option. 
    266 Use
    267 \f5set\ -o\fP
    268 to display the current option settings.
    269 Some of these options, such as
    270 .B interactive
    271 and
    272 .B monitor
    273 (see
    274 .I "Job Control"
    275 below),
    276 are enabled automatically by \f5ksh\fP
    277 when the shell is connected to a terminal device.
    278 Other options, such as
    279 .B noclobber
    280 and
    281 .BR ignoreeof ,
    282 are normally placed in a startup file.
    283 The
    284 .B noclobber
    285 option causes
    286 \f5ksh\fP
    287 to print an error message when you use
    288 .B >
    289 to redirect output to a file that already exists.
    290 If you want to redirect to an existing file, then
    291 you have to use
    292 .B >|
    293 to override
    294 the
    295 .B noclobber
    296 option.
    297 The
    298 .B ignoreeof
    299 option
    300 is used to prevent the
    301 .I end-of-file
    302 character, normally
    303 .B ^D
    304 (Control- d),
    305 from exiting the shell and possibly logging you out.
    306 You must type \f5exit\fP
    307 to log out.
    308 Most of the options are described in this memo as appropriate. 
    309 .H 2 "Command Aliases"
    310 .P
    311 Command aliases provide a mechanism of associating a command name and
    312 arguments with a shorter name.
    313 Aliases are defined with the \f5alias\fP
    314 built-in.
    315 The form of an \f5alias\fP
    316 command definition is:
    317 .ce
    318 \f5alias\fP \fIname\fP\f5=\fP\fIvalue\fP
    319 As with most other shell assignments, no space is allowed before or after
    320 the \f5=\fP.
    321 The characters of an alias name cannot be characters that are
    322 special to the shell. 
    323 The replacement string,
    324 .I value,
    325 can contain any valid shell script,
    326 including meta-characters such as pipe symbols and i/o-redirection
    327 provided that they are quoted.
    328 Unlike
    329 \f5csh\fP,
    330 aliases in
    331 \f5ksh\fP
    332 cannot take arguments.
    333 The equivalent functionality of aliases with arguments can
    334 be achieved with shell functions, described later.
    335 .P
    336 As a command is being read,
    337 the command name is checked against a list of
    338 .I alias
    339 names.
    340 If it is found,
    341 the name is replaced by the alias value associated with the
    342 .I alias
    343 and then rescanned.
    344 When rescanning the value for an alias, alias substitutions
    345 are performed except for an alias that is currently being processed.
    346 This prevents infinite loops in alias substitutions.
    347 For example with the aliases, \f5alias\ l=ls\ 'ls=ls\ -C'\fP,
    348 the command name \f5l\fP becomes \f5ls\fP, which becomes \f5ls\ -C\fP.
    349 Ordinarily, only the command name word is processed for alias substitution.
    350 However, if the value of an alias ends in a space,
    351 then the word following the alias is also checked for alias substitution.
    352 This makes it possible
    353 to define an alias whose first argument is the name of a command
    354 and have alias substitution performed on this argument,
    355 for example
    356 \f5nohup='nohup\ '\fP.
    357 .P
    358 Aliases can be used to redefine built-in commands so that
    359 the alias,
    360 .ce
    361 \f5alias test=./test\fP
    362 can be used to look for \f5test\fP
    363 in your current working directory rather than
    364 using the built-in \f5test\fP command.
    365 Reserved words such as
    366 \f5for\fP and \f5while\fP
    367 cannot be changed by aliasing.
    368 The command \f5alias\fP,
    369 without arguments, generates
    370 a list of aliases and corresponding alias values.
    371 The \f5unalias\fP command removes the name and text of an alias.
    372 .P
    373 Aliases are used to save typing and to improve readability of scripts.
    374 Several aliases are predefined by \f5ksh\fP.
    375 For example, the predefined alias
    376 .ce
    377 \f5alias integer='typeset -i'\fP
    378 allows the integer variables \f5i\fP and \f5j\fP
    379 to be declared and initialized with the command
    380 .ce
    381 \f5integer i=0 j=1\fP
    382 .P
    383 While aliases can be defined in scripts,
    384 it is not recommended.
    385 The location of an alias command can be important
    386 since aliases are only processed when a command is read.
    387 A \fB\s+2.\s-2\fP
    388 procedure (the shell equivalent of an include file)
    389 is read all at once (unlike
    390 start up files
    391 which are read a command at
    392 a time) so that any aliases defined there will not effect any commands
    393 within this script.
    394 Predefined aliases do not have this problem.
    395 .H 2 "Command Re-entry"
    396 .P
    397 When run interactively,
    398 \f5ksh\fP saves the
    399 commands you type at a terminal in a file.
    400 If the variable
    401 \fB\s-1HISTFILE\s+1\fP
    402 is set to the name of a file to which the user
    403 has write access,
    404 then the commands are stored in this
    405 .I history
    406 file.
    407 Otherwise the file
    408 \fB$\s-1HOME\s+1/.sh_history\fP
    409 is checked for write access and if this fails
    410 an unnamed file is used to hold the history lines.
    411 Commands are always appended to this file.
    412 Instances of \f5ksh\fP
    413 that run concurrently and use the same history file
    414 name, share access to the history file so that a command
    415 entered in one shell will be available for editing in another
    416 shell.
    417 The file may be truncated when \f5ksh\fP
    418 determines that no other shell is using the history file.
    419 The number of commands accessible to the user is determined by the value of the
    420 \fB\s-1HISTSIZE\s+1\fP
    421 variable at the time the shell is invoked.
    422 The default value is 256.
    423 Each command may consist of one or more lines since a compound
    424 command is considered one command.
    425 If the character
    426 .B !
    427 is placed within the
    428 .I "primary prompt"
    429 string,
    430 \fB\s-1PS1\s+1\fP,
    431 then it is replaced by the command number each time the prompt is given.
    432 .P
    433 A built-in command named \f5hist\fP
    434 is used to list and/or edit
    435 any of these saved commands.
    436 The option
    437 .B \-l
    438 is used to specify listing of previous commands.
    439 The command can always be specified with
    440 a range of one or more commands.
    441 The range can be specified by giving the command
    442 number, relative or absolute, or by giving
    443 the first character or characters of the command.
    444 When given without specifying the range,
    445 the last 16
    446 commands are listed, each
    447 preceded by the command number.
    448 .P
    449 If the listing option is not selected,
    450 then the range of commands specified,
    451 or the last command if no range is given,
    452 is passed to an editor program before
    453 being re-executed by \f5ksh\fP.
    454 The editor to be used may be specified
    455 with the option
    456 .B \-e
    457 and following it with the editor name.
    458 If this option is not specified, the
    459 value of the shell variable
    460 \fB\s-1HISTEDIT\s+1\fP
    461 is used as the name of the editor,
    462 providing that this variable has a non-null value.
    463 If this variable is not set, or is null,
    464 and the
    465 .B \-e
    466 option has not been selected,
    467 then
    468 \f5/bin/ed\fP
    469 is used.
    470 When editing has been complete,
    471 the edited text automatically becomes
    472 the input for \f5ksh\fP.
    473 As this text is read by \f5ksh\fP, it is echoed onto the terminal.
    474 .P
    475 The 
    476 .B \-s
    477 option causes the editing to be bypassed
    478 and just re-executes the command.
    479 In this case only a single command can be specified as the range
    480 and an optional argument of the form
    481 \fIold\fP\fB=\fP\fInew\fP
    482 may be added which requests a simple string substitution
    483 prior to evaluation.
    484 A convenient alias,
    485 .ce
    486 \f5alias r='hist -s'\fP
    487 has been pre-defined so that
    488 the single key-stroke
    489 \f5r\fP
    490 can be used to re-execute the previous command
    491 and the key-stroke sequence,
    492 \f5r\ abc=def\ c\fP
    493 can be used to re-execute the last command that starts with
    494 the letter \f5c\fP
    495 with the first occurrence of the string \f5abc\fP
    496 replaced with the string \f5def\fP.
    497 Typing
    498 \f5r\ c\ >\ file\fP
    499 re-executes the most recent command starting with the letter \f5c\fP,
    500 with standard output redirected to
    501 .IR file .
    502 .H 2 "In-line editing"
    503 .P
    504 Lines typed from a terminal frequently need changes made
    505 before entering them.
    506 With the Bourne shell the only method to fix up commands
    507 is by backspacing or killing the whole line.
    508 \f5ksh\fP offers options that allow the user to edit parts of the
    509 current command line before submitting the command.
    510 The in-line edit options make the command line into a single
    511 line screen edit window.
    512 When the command is longer than the width of the terminal,
    513 only a portion of the command is visible.
    514 Moving within the line automatically makes that portion visible.
    515 Editing can be performed on this window until the
    516 .I return
    517 key is pressed.
    518 The editing modes have editing directives that access the history file
    519 in which previous commands are saved.
    520 A user can copy any of the most recent
    521 \fB\s-1HISTSIZE\s+1\fP
    522 commands from this file into the input edit window.
    523 You can locate commands by searching or by position.
    524 .P
    525 The in-line editing options do not use the
    526 .I termcap
    527 or
    528 .I terminfo
    529 databases.
    530 They work on most standard terminals.
    531 They only require that the backspace character moves the cursor left
    532 and the space character overwrites the current character on the screen
    533 and moves the cursor to the right.
    534 Very few terminals or terminal emulators do not have
    535 this behavior.
    536 .P
    537 There is a choice of editor options.
    538 The
    539 .BR emacs ,
    540 .BR gmacs ,
    541 or
    542 .B vi
    543 option is selected by turning on the
    544 corresponding
    545 option of the \f5set\fP
    546 command.
    547 If the value of the
    548 \fB\s-1EDITOR\s+1\fP
    549 or
    550 \fB\s-1VISUAL\s+1\fP
    551 variables ends with any of these suffixes
    552 the corresponding option is turned on.
    553 A large subset of each of these editors'
    554 features is available within the shell.  Additional
    555 functions, such as file name completion, have also been added.
    556 .P
    557 In the
    558 .B emacs
    559 or
    560 .B gmacs
    561 mode the user positions the cursor to the point
    562 needing correction and inserts, deletes, or replaces
    563 characters as needed.
    564 The only difference between these two modes is the
    565 meaning of the directive
    566 .BR ^T .
    567 Control keys and escape sequences are used for cursor
    568 positioning and control functions.
    569 The available editing functions are listed in the manual page.
    570 .P
    571 The
    572 .B vi
    573 editing mode
    574 starts in insert mode and enters control mode when the
    575 user types ESC ( 033 ).
    576 The
    577 .I return
    578 key, which submits the current command for processing,
    579 can be entered from either mode.
    580 The cursor can be anywhere on the line.
    581 A subset of commonly used
    582 .I vi
    583 editing directives are available.
    584 The
    585 .B k
    586 and
    587 .B j
    588 directives that normally move up and down by one
    589 .IR line ,
    590 move up and down one
    591 .I command
    592 in the history file,
    593 copying the command into the input edit window.
    594 For reasons of efficiency,
    595 the terminal is kept in canonical mode until an
    596 ESC
    597 is typed.
    598 On some terminals,
    599 and on earlier versions of the UNIX operating system,
    600 this doesn't work correctly.
    601 The
    602 .B viraw
    603 option,
    604 which always uses
    605 .I raw
    606 or
    607 .I cbreak
    608 mode,
    609 must be used in this case.
    610 .P
    611 Most of the code for the editing options does not rely on the
    612 \f5ksh\fP code and can be used in a stand-alone mode with most any command
    613 to add in-line edit capability.
    614 However,
    615 all versions of the in-line editors have some features that
    616 use some shell specific code.  For example,
    617 with all edit modes, the
    618 ESC-=
    619 directive applied to command words
    620 (the first word on the line,
    621 or the first word after a
    622 .BR ; ,
    623 .BR | ,
    624 .BR ( ,
    625 or
    626 .BR & )
    627 lists all aliases, functions, or commands
    628 that match the portion of the given current word.
    629 When applied to other words, this directive
    630 prints the names of files that match the current
    631 word.
    632 The ESC\fB-*\fP directive
    633 adds the expanded list of matching files to the command line.
    634 A trailing
    635 .B *
    636 is added to the word if it doesn't contain any file pattern matching
    637 characters before the expansion.
    638 In
    639 .B emacs
    640 and
    641 .B gmacs
    642 mode,
    643 ESC-ESC
    644 indicates command completion when applied to
    645 command names, otherwise it indicates pathname completion.
    646 With command or pathname completion,
    647 the list generated by the
    648 ESC-= directive is examined to find
    649 the longest common prefix.
    650 With command completion, only the last component of
    651 the pathname is used to compute the longest command prefix.
    652 If the longest common prefix is a complete match,
    653 then the word is replaced by the pathname, and a
    654 .B /
    655 is appended if
    656 pathname is a directory, otherwise a space is added.
    657 In
    658 .B vi
    659 mode,
    660 .B \e
    661 from control mode gives the same behavior.
    662 .H 2 "Key Binding"
    663 .P
    664 It is possible to intercept keys as they are entered and
    665 apply new meanings or bindings.
    666 A trap named
    667 \fB\s-1KEYBD\s+1\fP
    668 is evaluated each time
    669 \f5ksh\fP processes characters entered
    670 from the keyboard,  
    671 other than those typed
    672 while entering a search string or an argument to an
    673 edit directive such as
    674 .B r
    675 in vi-mode.
    676 The action associated with this trap can change the value of
    677 the entered key to cause the key to perform a different
    678 operation.
    679 .P
    680 When the
    681 \fB\s-1KEYBD\s+1\fP
    682 trap is entered,
    683 the \fB.sh.edtext\fP
    684 variable contains the contents of the current input line
    685 and the \fB.sh.edcol\fP
    686 variable gives the current cursor position within this line.
    687 The \fB.sh.edmode\fP
    688 variable contains the
    689 .B ESC
    690 character when the trap is entered from
    691 .B vi
    692 insert mode.
    693 Otherwise, this value is null.
    694 The \fB.sh.edchar\fP
    695 variable contains the character or
    696 escape sequence that caused the trap.
    697 A key sequence is either a single character,
    698 .B ESC
    699 followed by a single character,
    700 or
    701 .B ESC[
    702 followed by a single character.
    703 In the \fBvi\fP edit mode,
    704 the characters after the
    705 .B ESC
    706 must be entered within half a second after the
    707 .BR ESC .
    708 The value of \fB.sh.edchar\fP
    709 at the end of the trap will be used as
    710 the input sequence. 
    711 .P
    712 Using the associative array facility of \f5ksh\fP described later,
    713 and the function facility of \f5ksh\fP, it is easy to write
    714 a single trap so that keys can be bound dynamically.  For example,
    715 .sp
    716 .nf
    717 .in .5i
    718 .ta 4i
    719 \f5typeset -A Keytable
    720 trap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD
    721 function keybind # key action
    722 { 
    723         typeset key=$(print -f "%q" "$2")
    724         case $# in
    725         2)      Keytable[$1]='.sh.edchar=${.sh.edmode}'"$key"
    726                 ;;
    727         1)      unset Keytable[$1]
    728                 ;;
    729         *)      print -u2 "Usage: $0 key [action]"
    730                 ;;
    731         esac
    732 }\fP
    733 .ta
    734 .in
    735 .fi
    736 .sp
    737 .H 2 "Job Control"
    738 .P
    739 The job control mechanism 
    740 is almost identical to the version introduced in \f5csh\fP
    741 of the Berkeley UNIX operating system,
    742 version 4.1 and later.
    743 The job control feature allows the user to stop and
    744 restart programs, and to move programs to and from the
    745 foreground and the background.
    746 It will only work on systems that provide support for
    747 these features.
    748 However,
    749 even systems without job control have a
    750 .B monitor
    751 option which, when enabled, will report the progress
    752 of background jobs and enable the user to \f5kill\fP
    753 jobs by job number or job name.
    754 .P
    755 An interactive shell associates a
    756 .I job
    757 with each pipeline typed in from the terminal
    758 and assigns it a small integer number
    759 called the job number.
    760 If the job is run asynchronously,
    761 the job number is printed at the terminal.
    762 At any given time, only one job owns the terminal,
    763 i.e., keyboard signals are only sent to the processes in one job.
    764 When \f5ksh\fP creates a foreground job,
    765 it gives it ownership of the terminal.
    766 If you are running a job and wish to stop
    767 it you hit the key
    768 .B ^Z
    769 (control-\fBZ\fP)
    770 which sends a
    771 \fB\s-1STOP\s+1\fP
    772 signal to all processes in the current job.
    773 The shell receives notification that the processes
    774 have stopped and takes back control of the terminal.
    775 .P
    776 There are commands to continue programs in the foreground
    777 and background.
    778 There are several ways to refer to jobs.
    779 The character
    780 .B %
    781 introduces a job name.
    782 You can refer to jobs by name or number as described in the manual page.
    783 The built-in command \f5bg\fP
    784 allows you to continue a job in the background,
    785 while the built-in command \f5fg\fP
    786 allows you to continue a job in the foreground even
    787 though you may have started it in the background.
    788 .P
    789 A job being run in the background will stop if it tries
    790 to read from the terminal.
    791 It is also possible to stop background jobs that try to write on
    792 the terminal by setting the terminal options
    793 appropriately.
    794 .P
    795 There is a built-in command \f5jobs\fP
    796 that lists the status of all running and stopped jobs.
    797 In addition,
    798 you are informed of the change of state (running or stopped)
    799 of any background
    800 jobs just before each prompt.
    801 If you want to be notified about background job completions
    802 as soon as they occur without waiting for a prompt, then use the
    803 .B notify
    804 option.
    805 When you try to exit the shell while jobs are stopped or running,
    806 you will receive a message from \f5ksh\fP.
    807 If you ignore this message and try to exit again,
    808 all stopped processes will be terminated.
    809 In addition, for login shells, the
    810 \fB\s-1HUP\s+1\fP
    811 signal will be sent to
    812 all background jobs
    813 unless the job has been disowned with the
    814 .B disown
    815 command.
    816 .P
    817 A built-in version of \f5kill\fP
    818 makes it possible to use
    819 .I job
    820 numbers as targets for signals.
    821 Signals can be selected by number or name.
    822 The name of the signal is the name found in the
    823 .I include
    824 file
    825 .B /usr/include/sys/signal.h
    826 with the prefix
    827 .B \s-1SIG\s+1
    828 removed.
    829 The
    830 .B \-l
    831 option of \f5kill\fP 
    832 provides a means to map individual signal names to and from
    833 signal number.
    834 In addition, if no signal name or number is given,
    835 \f5kill\ -l\fP 
    836 generates a list of valid signal names.
    837 .H 2 "Changing Directories"
    838 By default,
    839 \f5ksh\fP
    840 maintains a logical view of the file system hierarchy
    841 which makes symbolic links transparent.
    842 For systems that have symbolic links,
    843 this means that if \f5/bin\fP is a symbolic link to \f5/usr/bin\fP
    844 and you change directory to \f5/bin\fP, \f5pwd\fP will indicate
    845 that you are in \f5/bin\fP, not \f5/usr/bin\fP. 
    846 \f5pwd\ -P\fP
    847 generates the physical pathname of the present working
    848 directory by resolving all the symbolic links.
    849 By default,
    850 the \f5cd\fP
    851 command will take you where you expect to go even if you cross
    852 symbolic links.
    853 A subsequent \f5cd\ ..\fP in the example above
    854 will place you in \f5/\fP, not \f5/usr\fP.
    855 On systems with symbolic links,
    856 \f5cd\ -P\fP
    857 causes
    858 .B ..
    859 to be treated physically.
    860 .P
    861 \f5ksh\fP remembers your last directory
    862 in the variable
    863 \fB\s-1OLDPWD\s+1\fP.
    864 The \f5cd\fP
    865 built-in can be given with argument
    866 .B \-
    867 to return to the previous directory
    868 and print the name of the directory.
    869 Note that \f5cd\ -\fP
    870 done twice returns you to the starting directory,
    871 not the second previous directory.
    872 A directory
    873 .I stack
    874 manager has been written as shell
    875 .I functions
    876 to
    877 .I push
    878 and
    879 .I pop
    880 directories from the stack.
    881 .H 2 "Prompts"
    882 .P
    883 When \f5ksh\fP
    884 reads commands from a terminal,
    885 it issues a prompt whenever it is ready
    886 to accept more input and then
    887 waits for the user to respond.
    888 The
    889 \fB\s-1TMOUT\s+1\fP
    890 variable
    891 can be set to be the number of seconds that the shell will wait for
    892 input before terminating.
    893 A 60 second warning message is printed
    894 before terminating.
    895 .P
    896 The shell uses two prompts.
    897 The primary prompt,
    898 defined by the value of the
    899 \fB\s-1PS1\s+1\fP
    900 variable,
    901 is issued at the start of each command.
    902 The secondary prompt,
    903 defined by the value of the
    904 \fB\s-1PS2\s+1\fP
    905 variable,
    906 is issued when more input is needed to complete a command.
    907 .P
    908 \f5ksh\fP allows the user to specify a list of files or directories
    909 to check before issuing the
    910 \fB\s-1PS1\s+1\fP
    911 prompt.
    912 The variable
    913 \fB\s-1MAILPATH\s+1\fP
    914 is a colon (
    915 .B :
    916 ) separated list of file names to be checked for changes
    917 periodically. The user is notified
    918 before the next prompt.
    919 Each of the names in this list can be followed by a
    920 .B ?
    921 and a message to be given when a change has been detected in the file.
    922 The prompt will be evaluated for parameter expansion, command
    923 substitution and arithmetic expansion which are described later.
    924 The parameter
    925 .B $_
    926 within a mail message will evaluate to the name of the file that
    927 has changed.
    928 The parameter
    929 \fB\s-1MAILCHECK\s+1\fP
    930 is used to specify the minimal interval in seconds before
    931 new mail is checked for.
    932 .P
    933 In addition to replacing each
    934 .B !
    935 in the prompt with the command number,
    936 \f5ksh\fP expands
    937 the value of the
    938 .B \s-1PS1\s+1
    939 variable
    940 for parameter expansions, arithmetic expansions,
    941 and command substitutions as described below
    942 to generate the prompt.
    943 The expansion characters that are to be applied when
    944 the prompt is issued must be quoted to prevent the
    945 expansions from occurring when assigning the value to
    946 .B \s-1PS1\s+1.
    947 For example,
    948 \f3\s-1PS1\s+1="$\s-1PWD\s+1"\fP
    949 causes 
    950 .B \s-1PS1\s+1
    951 to be set to the value of
    952 .B \s-1PWD\s+1
    953 at the time of the assignment whereas
    954 .B \s-1PS1\s+1='$\s-1PWD\s+1'
    955 causes
    956 .B \s-1PWD\s+1
    957 to be expanded at the time the prompt is issued.
    958 .P
    959 Command substitution may require a separate process
    960 to execute and cause the prompt display to be somewhat
    961 slow, especially
    962 when the return key is pressed several times in a row.
    963 Therefore, its use
    964 within
    965 .B \s-1PS1\s+1
    966 is discouraged.
    967 Some variables are maintained by \f5ksh\fP
    968 so that their values can be used with
    969 .B \s-1PS1\s+1.
    970 The
    971 .B \s-1PWD\s+1
    972 variable stores the pathname of the current working directory.
    973 The value of 
    974 .B \s-1SECONDS\s+1
    975 variable
    976 is the value of the most
    977 recent assignment plus the elapsed time.
    978 By default, the time is measured in milli-seconds,
    979 but since
    980 .B \s-1SECONDS\s+1
    981 is a floating point variable, the
    982 number of places after the decimal point in the expanded
    983 value can be
    984 specified with
    985 \f5typeset\ -F\fP\fIplaces\fP\f5\ SECONDS\fP.
    986 In a roundabout way, this variable
    987 can be used to generate a time stamp into the
    988 .B \s-1PS1\s+1
    989 prompt without creating a process at each prompt.
    990 The following code explains how you can do this on
    991 System V.  On BSD, you need a different command to initialize
    992 the
    993 .B \s-1SECONDS\s+1
    994 variable.
    995 \f5
    996 .sp
    997 .nf
    998 .in .5i
    999 # . this script and use $TIME as part of your PS1 string to
   1000 # get the time of day in your prompt
   1001 typeset -RZ2  _x1 _x2 _x3
   1002 (( SECONDS=$(date  '+3600*%H+60*%M+%S') ))
   1003 _s='_x1=(SECONDS/3600)%24,_x2=(SECONDS/60)%60,_x3=SECONDS%60,0'
   1004 TIME='"${_d[_s]}$_x1:$_x2:$_x3"'
   1005 # PS1=${TIME}whatever
   1006 .fi
   1007 .ta
   1008 .in
   1009 .sp
   1010 \fP
   1011 .H 2 "Tilde substitution"
   1012 .P
   1013 The character
   1014 .B \(ap
   1015 at the beginning of a word has special meaning to \f5ksh\fP.
   1016 If the characters after the
   1017 .B \(ap
   1018 up to a
   1019 .B /
   1020 match a user login name in the password database, then the
   1021 .B \(ap
   1022 and the name are replaced by
   1023 that user's login directory.
   1024 If no match is found, the original word
   1025 is unchanged.
   1026 A
   1027 .B \(ap
   1028 by itself, or in front of a
   1029 .BR / ,
   1030 is replaced by the value of the
   1031 \fB\s-1HOME\s+1\fP
   1032 parameter.
   1033 A
   1034 .B \(ap
   1035 followed by a
   1036 .B +
   1037 or
   1038 .B \-
   1039 is replaced by the value of
   1040 .B $\s-1PWD\s+1
   1041 or
   1042 .B $\s-1OLDPWD\s+1
   1043 respectively.
   1044 .H 2 "Output formats"
   1045 The output of built-in commands and traces have values quoted so that they
   1046 can be re-input to the shell.
   1047 This makes it easy to cut and paste shell output on systems
   1048 which use a pointing device such as a mouse.
   1049 In addition, output can be saved in a file for reuse.
   1050 .P
   1051 .H 2 "The \fB\s-1ENV\s+1\fP file"
   1052 When an interactive \f5ksh\fP starts, it evaluates the
   1053 .B $\s-1ENV\s+1
   1054 variable to arrive at a file name.
   1055 If this value is not null,
   1056 \f5ksh\fP attempts to read and process
   1057 commands in a file by this name.
   1058 Earlier versions of \f5ksh\fP read the \fB\s-1ENV\s+1\fP file
   1059 for all invocations of the shell primarily to allow
   1060 function definitions to be available for all shell
   1061 invocations.
   1062 The function search path, \fB\s-1FPATH\s+1\fP, described later,
   1063 eliminated the primary need for this capability and it was
   1064 removed because the high performance cost was no longer
   1065 deemed acceptable.
   1066 .H 1 "PROGRAMMING LANGUAGE"
   1067 The KornShell vastly extends the set of applications that
   1068 can be implemented efficiently at the shell level.
   1069 It does this by providing simple yet powerful mechanisms
   1070 to perform arithmetic, pattern matching,
   1071 substring generation,
   1072 and arrays.
   1073 Users can write applications as separate functions that can
   1074 be defined in the same file or in a library of functions
   1075 stored in a directory and loaded on demand.
   1076 .H 2 "String Processing"
   1077 The shell is primarily a string processing language.
   1078 By default, variables hold variable length strings.
   1079 There are no limits to the length of strings.  Storage
   1080 management is handled by the shell automatically.
   1081 Declarations are not required.
   1082 With most programming languages, string constants are designated
   1083 by enclosing characters in single quotes or double quotes.
   1084 Since most of the words in the language are strings, the shell
   1085 requires quotes only when a string contains characters that
   1086 are normally processed specially by the shell, but their
   1087 literal meaning is intended.
   1088 However, since the shell is a string processing language,
   1089 and some characters can occur as literals and as language metacharacters,
   1090 quoting is an important part of the language.
   1091 .P
   1092 There are four quoting mechanisms in \f5ksh\fP.
   1093 The simplest is to enclose a sequence of characters inside single quotes.
   1094 All characters between a pair of single quotes have their literal meaning;
   1095 the single quote itself cannot appear.
   1096 A
   1097 .B $
   1098 immediately preceding
   1099 a single quoted string
   1100 causes all the characters until the matching single quote
   1101 to be interpreted as an ANSI-C language string.
   1102 Thus, \f5'\en'\fP represents characters \f5\e\fP and
   1103 \f5n\fP, whereas, \f5$'\en'\fP 
   1104 represents the new-line character.
   1105 Double quoted strings remove the special meaning of all characters
   1106 except
   1107 .BR $ ,
   1108 .BR \(ga ,
   1109 and
   1110 .BR \e ,
   1111 so that parameter expansion and command substitution (defined below)
   1112 are performed.
   1113 The final mechanism for quoting a character is by preceding it with the
   1114 escape character
   1115 .BR \e\^ .
   1116 This mechanism works outside of quoted strings and for the characters
   1117 .BR $ ,
   1118 .BR \(ga ,
   1119 \fB"\fP,
   1120 and
   1121 .B \e
   1122 in double quoted strings.
   1123 .P
   1124 Variables are designated by
   1125 one or more
   1126 strings of alphanumeric
   1127 characters beginning with an alphabetic character
   1128 separated by a \fB\s+2.\s-2\fP.
   1129 Upper and lower case characters are distinct, so that the variable
   1130 .B A
   1131 and
   1132 .B a
   1133 are names of different variables.
   1134 There is no
   1135 limit to the length of the name of a variable.
   1136 You do not have to declare variables.
   1137 You can assign a value to a variable by writing the name of the
   1138 variable, followed by an equal sign, followed by a character string
   1139 that represents its value.
   1140 To create a variable whose name
   1141 contains a \fB\s+2.\s-2\fP,
   1142 the variable whose name consists of
   1143 the characters before the last \fB\s+2.\s-2\fP
   1144 must already exist.
   1145 You reference a variable by
   1146 putting the name inside curly braces and
   1147 preceding the braces with a dollar sign.
   1148 The braces may be omitted when the name
   1149 is alphanumeric.
   1150 If \f5x\fP and \f5y\fP
   1151 are two shell variables, then
   1152 to define a new variable,
   1153 \f5z\fP,
   1154 whose value is
   1155 the concatenation of the values of
   1156 \f5x\fP and \f5y\fP,
   1157 you just say
   1158 \f5z=$x$y\fP.
   1159 It is that easy.
   1160 .P
   1161 The
   1162 .B $
   1163 can be thought of as meaning
   1164 "value of."
   1165 You can also capture the output of any command with the notation
   1166 .BI $( command ) .
   1167 This is referred to as command substitution.
   1168 For example,
   1169 \f5x=$(date)\fP
   1170 assigns the output from the \f5date\fP
   1171 command to the variable \f5x\fP.
   1172 Command substitution in the
   1173 Bourne shell is denoted by enclosing the command between
   1174 backquotes,
   1175 (\fB\(ga\^\(ga\fP).
   1176 This notation
   1177 suffers from some
   1178 complicated quoting rules.
   1179 Thus, it is hard to write \f5sed\fP
   1180 patterns which contains back slashes within command substitution.
   1181 Putting the pattern in single quotes
   1182 is of little help.
   1183 \f5ksh\fP accepts the Bourne shell command substitution syntax
   1184 for backward compatibility.
   1185 The
   1186 .BI $( command )
   1187 notation allows
   1188 the \fIcommand\fP itself to contain quoted strings even if the substitution
   1189 occurs within double quotes. Nesting is legal.
   1190 .P
   1191 The special command substitution of the form
   1192 \f5$(cat\ file)\fP
   1193 can be replaced by
   1194 \f5$(<\ file)\fP,
   1195 which is faster because
   1196 the \f5cat\fP
   1197 command doesn't have to run.
   1198 .H 2 "Shell Parameters and Variables"
   1199 .P
   1200 There are three types of parameters used by \f5ksh\fP,
   1201 special parameters, positional parameters, and named
   1202 parameters which are called variables.
   1203 \f5ksh\fP defines the same special parameters,
   1204 .BR 0 ,
   1205 .BR * ,
   1206 .BR @ ,
   1207 .BR # ,
   1208 .BR ? ,
   1209 .BR $ ,
   1210 .BR ! ,
   1211 and
   1212 .BR \- ,
   1213 as in the Bourne shell.
   1214 .P
   1215 Positional parameters are set when the shell is invoked,
   1216 as arguments to the \f5set\fP built-in,
   1217 and by calls to functions (see below) and \fB\s+2.\s-2\fP
   1218 procedures.
   1219 They are named by numbers starting at 1.
   1220 .P
   1221 The third type of parameter is a variable.
   1222 As mentioned earlier,
   1223 \f5ksh\fP uses variables whose names
   1224 consist of one or more
   1225 alpha-numeric strings separated by a \fB\s+2.\s-2\fP.
   1226 There is no need to specify the
   1227 .I type
   1228 of a variable in the shell because, by default,
   1229 variables store strings of arbitrary length
   1230 and values will automatically be converted to numbers
   1231 when used in an arithmetic context.
   1232 However, \f5ksh\fP variables
   1233 can have one or more
   1234 .I attributes
   1235 that control the internal representation of the variable,
   1236 the way the variable is printed, and its access or
   1237 scope.
   1238 In addition,
   1239 \f5ksh\fP
   1240 allows variables to represent arrays of values
   1241 and references to other variables.
   1242 The \f5typeset\fP
   1243 built-in command of \f5ksh\fP
   1244 assigns attributes to variables.
   1245 Two of the attributes,
   1246 .I readonly
   1247 and
   1248 .IR export ,
   1249 are available in the Bourne shell.
   1250 Most of the remaining attributes are discussed here.
   1251 The complete list of attributes appears in the manual.
   1252 The \f5unset\fP
   1253 built-in of \f5ksh\fP removes
   1254 values and attributes of variables.
   1255 When a variable is exported, certain of its attributes are also exported.
   1256 .P
   1257 Whenever a value is assigned to a variable,
   1258 the value is transformed according to the attributes of the variable.
   1259 Changing the attribute of a variable can change its value.
   1260 The attributes
   1261 .B \-L
   1262 and
   1263 .B \-R
   1264 are for left and right field justification respectively.
   1265 They are useful for aligning columns in a report.
   1266 For each of these attributes, a width can be defined explicitly or else
   1267 it is defined the first time an assignment is made to the variable.
   1268 Each assignment causes justification of the field, truncating
   1269 if necessary.
   1270 Assignment to fixed sized variables
   1271 provides one way to generate a substring consisting of
   1272 a fixed number of characters from
   1273 the beginning or end of a string.
   1274 Other methods are discussed later.
   1275 .P
   1276 The attributes
   1277 .B \-u
   1278 and
   1279 .B \-l
   1280 are used for upper case and lower case
   1281 formatting, respectively.
   1282 Since it makes no sense to have both attributes on simultaneously,
   1283 turning on either of these attributes turns the other off.
   1284 The following script,
   1285 using \f5read\fP and \f5print\fP which are described later,
   1286 provides an example of the use of shell variables
   1287 with attributes.
   1288 This script reads a file of lines each consisting of five fields separated by
   1289 .B :
   1290 and prints fields 4 and 2 in upper case in columns 1-15, left justified,
   1291 and columns 20-25 right-justified respectively.
   1292 .sp
   1293 .nf
   1294 .in .5i
   1295 .ta 3.4i
   1296 \f5typeset -uL15 f4                # 15 character left justified
   1297 typeset -uR6 f2                 # 6 character right justified
   1298 IFS=:                           # set field separator to :
   1299 while   read -r f1 f2 f3 f4 f5  # read line, split into fields
   1300 do      print -r -- "$f4  $f2"  # print fields 4 and 2
   1301 done\fP
   1302 .fi
   1303 .ta
   1304 .in
   1305 .sp
   1306 .P
   1307 The
   1308 .BR \-i ,
   1309 .BR \-E ,
   1310 and
   1311 .BR \-F ,
   1312 attributes are used to represent numbers.
   1313 Each can be followed by a decimal number.
   1314 The
   1315 .B \-i
   1316 attribute causes the value to be represented as an integer and it
   1317 can be followed by a number representing the numeric base when expanding
   1318 its value.
   1319 Whenever a value is assigned to an integer variable, it is evaluated
   1320 as an arithmetic expression
   1321 and then truncated to an integer.
   1322 .P
   1323 The
   1324 .B \-E
   1325 attribute causes the value to be represented in scientific
   1326 notation whenever its value is expanded.  The number following the 
   1327 .B \-E
   1328 determines the number of significant figures, and defaults to 6.
   1329 The
   1330 .B \-F
   1331 attribute causes the value to be represented with a fixed number
   1332 of places after the decimal point. 
   1333 Assignments to variables with the
   1334 .B \-E
   1335 or
   1336 .B \-F
   1337 attributes cause the evaluation of the right hand side of the assignment.
   1338 .P
   1339 \f5ksh\fP allows one-dimensional
   1340 .I arrays
   1341 in addition to simple variables.
   1342 There are two types of arrays; associative arrays
   1343 and indexed arrays.
   1344 The subscript for an associative array is an arbitrary
   1345 string, whereas the subscript for an indexed array is
   1346 an arithmetic expression that is evaluated to yield an integer
   1347 index.
   1348 Any variable can become an indexed array
   1349 by referring to it with
   1350 an integer
   1351 .IR subscript .
   1352 All elements of an array need not exist.
   1353 Subscripts for arrays
   1354 must evaluate to an
   1355 integer between 0 and some maximum value, otherwise
   1356 an error results.
   1357 The maximum value may vary from one machine to another but
   1358 is at least 4095.
   1359 Evaluation of subscripts is described in
   1360 the next section.
   1361 Attributes apply to the whole array.
   1362 .P
   1363 Assignments to array variables can be made to individual elements
   1364 via parameter
   1365 assignment commands or the
   1366 .B typeset
   1367 built-in.
   1368 Additionally, values can be assigned sequentially with
   1369 compound assignment as described below, or by the
   1370 .B \-A
   1371 .I name
   1372 option of the \f5set\fP command.
   1373 Referencing of subscripted variables requires the character
   1374 .BR $ ,
   1375 but also requires braces around the array element name.
   1376 The braces are needed to avoid conflicts with the
   1377 file name generation mechanism.
   1378 The form of any array element reference is:
   1379 .ce
   1380 .BI ${ name [ subscript ]}
   1381 Subscript values of
   1382 .B *
   1383 and
   1384 .B @
   1385 can be used to generate all elements of an array,
   1386 as they are used for expansion of positional parameters.
   1387 The list of currently defined subscripts for a given
   1388 variable can be generated with 
   1389 .BI ${! name [@]} ,
   1390 or
   1391 .BI ${! name [*]} .
   1392 .P
   1393 The
   1394 .B \-n
   1395 or
   1396 .I nameref
   1397 attribute causes the variable to be treated
   1398 as a reference to the variable defined by its value.
   1399 Once this attribute is set, all references to this variable
   1400 become references to the variable named by the value
   1401 of this variable.
   1402 For example, if \f5foo=bar\fP, then setting the reference
   1403 attribute on \f5foo\fP will cause all subsequent references
   1404 to \f5foo\fP to behave as the variable whose name is \f5$foo\fP
   1405 was referenced, which in this case is the variable \f5bar\fP.
   1406 Unsetting this attribute breaks the association.
   1407 Reference variables are usually used inside functions whose
   1408 arguments are the names of shell variables. 
   1409 The names for reference variables cannot contain a \fB\s+2.\s-2\fP.
   1410 Whenever a shell variable is referenced, the portion of the
   1411 variable up to the first \fB\s+2.\s-2\fP
   1412 is checked to see whether it matches the name of a reference
   1413 variable.
   1414 If it does, then the name of the variable actually used
   1415 consists of the concatenation of the name of the variable
   1416 defined by the reference plus the remaining portion of the
   1417 original variable name.
   1418 For example, using the predefined alias, \f5alias\ nameref='typeset\ -n'\fP,
   1419 .sp
   1420 .nf
   1421 .in .5i
   1422 .ta 3.4i
   1423 \f5\^.bar.home.bam="hello world"
   1424 nameref foo=.bar.home
   1425 print ${foo.bam}
   1426 \fBhello world\fP\fP
   1427 .fi
   1428 .ta
   1429 .in
   1430 .sp
   1431 .H 2 "Compound Assignment"
   1432 Compound assignments are used to assign values to arrays
   1433 and compound data structures.
   1434 The syntax for a compound assignment is
   1435 .IB name =( assignment-list )
   1436 where
   1437 \fIname\fP
   1438 is the name of the variable to which you want to assign values.
   1439 No space is permitted between the variable name and the \fB=\fP
   1440 but can appear between the \fB=\fP and the open parenthesis.
   1441 New-lines can appear between the parentheses.
   1442 .P
   1443 The \fIassignment-list\fP can be in several different forms
   1444 yielding different results.
   1445 If \fIassignment-list\fP is simply a list of words, then
   1446 the words are processed as they are with the \f5for\fP command
   1447 and assigned sequentially as an indexed array.
   1448 For example,
   1449 .ce
   1450 \f5foo=( * )\fP
   1451 creates an indexed array \f5foo\fP and assigns the
   1452 file names in the current directory to each index starting
   1453 at zero.
   1454 .P
   1455 The second form for \fIassignment-list\fP is a list of assignments
   1456 of the special form \fB[\fP\fIword\fP\fB]=\fP\fIword\fP.
   1457 No space is permitted before or after the \fB=\fP.
   1458 In this case, the variable given by \fIname\fP becomes
   1459 an associative array with the given arguments as subscripts.
   1460 For example,
   1461 .ce
   1462 \f5bar=( [color]=red [shape]=box )\fP
   1463 creates an associate array named \f5bar\fP whose
   1464 subscripts are \f5color\fP and \f5shape\fP.
   1465 .P
   1466 The third form for \fIassignment-list\fP is a list of
   1467 normal assignments, including compound assignments.
   1468 These assignments cause sub-variables to be assigned
   1469 corresponding to the given assignments.
   1470 In addition to assignments, the \fIassignment-list\fP
   1471 can contain \f5typeset\fP commands.
   1472 In addition to creating sub-variables,
   1473 the effect of a compound assignment is to make
   1474 the value of the original variable be a parenthesized
   1475 assignment list of its components.
   1476 For example, the assignment
   1477 .sp
   1478 .nf
   1479 .in .5i
   1480 .ta 3.4i
   1481 \f5foo=(
   1482         left=bar
   1483         typeset -i count=3
   1484         point=(
   1485                 x=50
   1486                 y=60
   1487         )
   1488         colors=( red green yellow )
   1489         right=bam
   1490 ) \fP
   1491 .ta
   1492 .in
   1493 .fi
   1494 .sp
   1495 is equivalent to the assignments
   1496 .sp
   1497 .nf
   1498 .in .5i
   1499 .ta 3.4i
   1500 \f5foo.left=bar
   1501 foo.count=3
   1502 foo.point.x=50
   1503 foo.point.y=60
   1504 foo.colors=( red green yellow )
   1505 foo.right=bam\fP
   1506 .ta
   1507 .in
   1508 .fi
   1509 .sp
   1510 In addition, the value of \f5"$foo"\fP is
   1511 .sp
   1512 .nf
   1513 .in .5i
   1514 .ta 3.4i
   1515 \f5(
   1516         colors=( red green yellow )
   1517         left=bar
   1518         typeset -i count=3
   1519         point=(
   1520                 y=60
   1521                 x=50
   1522         )
   1523         right=bam
   1524 )\fP
   1525 .ta
   1526 .in
   1527 .fi
   1528 .sp
   1529 .H 2 "Substring Generation"
   1530 The expansion of a variable or parameter can be modified so that
   1531 only a portion of the value results. 
   1532 It is often necessary to extract a portion of a shell variable or
   1533 a portion of an array.
   1534 There are several parameter expansion operators that can do this.
   1535 One method to generate a substring is with an expansion of
   1536 the form \fB${\fP\fIname\fP\fB:\fP\fIoffset\fP\fB:\fP\fIlength\fP\fB}\fP
   1537 where \fIoffset\^\fP is an arithmetic expression that defines the
   1538 offset of the first character starting from 0, and
   1539 \fIlength\^\fP is an arithmetic expression that defines the
   1540 length of the substring.
   1541 If
   1542 .BI : length\^
   1543 is omitted,
   1544 the length of the value of
   1545 .I name\^
   1546 starting at
   1547 .I offset\^
   1548 is used.
   1549 The
   1550 .BI : offset : length
   1551 operators can also be applied to array expansions and to parameters
   1552 .B *
   1553 and
   1554 .B @
   1555 to generate portions of an array.
   1556 For example, the expansion, \fB${\fP\fIname\fP\fB[@]:\fP\fIoffset\fP\fB:\fP\fIlength\fP\fB}\fP, yields up to \fIlength\fP elements of the array \fIname\fP
   1557 starting at the element \fIoffset\fP.
   1558 .P
   1559 The other parameter expansion modifiers use shell patterns
   1560 to describe portions of the string to modify and delete.
   1561 A description of shell patterns is contained below.
   1562 When these
   1563 modifiers are applied to special parameters
   1564 .B @
   1565 and
   1566 .B *
   1567 or to array parameters given as
   1568 \fIname\fP\fB[@]\fP or \fIname\fP\fB[*]\fP,
   1569 the operation is performed on each element.
   1570 There are four parameter expansion modifiers that
   1571 strip off leading and trailing substrings
   1572 during parameter expansion
   1573 by removing the characters matching a given pattern.
   1574 An expansion of
   1575 the form \fB${\fP\fIname\fP\fB#\fP\fIpattern\fP\fB}\fP
   1576 causes the smallest matching prefix of the value of
   1577 .I name\^
   1578 to be removed.
   1579 The largest prefix matching
   1580 .I pattern\^
   1581 is removed by using
   1582 .B ##
   1583 instead of
   1584 .BR # .
   1585 Similarly,
   1586 an expansion of
   1587 the form \fB${\fP\fIname\fP\fB%\fP\fIpattern\fP\fB}\fP
   1588 causes the smallest matching substring at the end of
   1589 .I name\^
   1590 to be removed.
   1591 Again, using
   1592 .B %%
   1593 instead of
   1594 .BR % ,
   1595 causes the largest matching trailing substring to be deleted.
   1596 For example, if the shell variable
   1597 .B file
   1598 has value
   1599 .BR foo.c ,
   1600 then the expression
   1601 .B ${file%.c}.o
   1602 has value
   1603 .BR foo.o .
   1604 .P
   1605 The value of an expansion can be changed by
   1606 specifying a pattern that matches the part that needs to be changed
   1607 after the
   1608 the parameter expansion modifier
   1609 .BR / .
   1610 An expansion of the form
   1611 \fB${\fP\fIname\fP\fB/\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
   1612 replaces the first match of \fIpattern\fP with
   1613 the value of variable \fIname\fP to \fIstring\fP.
   1614 The second
   1615 .B /
   1616 is not necessary when \fIstring\fP is null.
   1617 The expansion
   1618 \fB${\fP\fIname\fP\fB//\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
   1619 changes all occurrences of the \fIpattern\fP into \fIstring\fP.
   1620 The parameter expansion modifiers
   1621 .B /#
   1622 and
   1623 .B /%
   1624 cause the matching pattern to be anchored to the beginning and
   1625 end respectively.
   1626 .P
   1627 Finally, there are parameter expansion modifiers that yield
   1628 the name of the variable, the string length of the value, or the number
   1629 of elements of an array.
   1630 \fB${!\fP\fIname\fP\fB}\fP
   1631 yields the name of the variable which will be \fIname\fP itself
   1632 except when \fIname\fP is a reference variable.  In this case
   1633 it will yield the name of the variable it refers to.
   1634 When applied to an array variable,
   1635 \fB${!\fP\fIname\fP\fB[@]}\fP and
   1636 \fB${!\fP\fIname\fP\fB[*]}\fP 
   1637 generate the names of all subscripts.
   1638 \fB${#\fP\fIname\fP\fB}\fP
   1639 will be the length in bytes of
   1640 \fB$\fP\fIname\fP.
   1641 For an array variable
   1642 \fB${#\fP\fIname\fP\fB[*]}\fP
   1643 gives the number of elements in the array.
   1644 .H 2 "Arithmetic Evaluation"
   1645 .P
   1646 For the most part, the shell is a string processing
   1647 language.  However, the need for arithmetic has
   1648 long been obvious.
   1649 Many of the characters that are special to the
   1650 Bourne shell are needed as arithmetic operators.
   1651 To make arithmetic easy to use, and to maintain
   1652 compatibility with the Bourne shell, \f5ksh\fP uses matching
   1653 .B ((
   1654 and
   1655 .B ))
   1656 to delineate arithmetic expressions.
   1657 While single parentheses might have been
   1658 more desirable, these already mean
   1659 .I subshell\^
   1660 so that another notation was required.
   1661 The arithmetic expression
   1662 inside the double parentheses
   1663 follows the same syntax, associativity and precedence
   1664 as the ANSI-C\*(Rf
   1665 .RS
   1666 American National Standard for Information Systems \- Programming
   1667 Language \- C, ANSI X3.159-1989.
   1668 .RF
   1669 programming language.
   1670 The characters between the matching double parentheses
   1671 are processed with the same rules used for double
   1672 quotes so that spaces can be used to aid readability
   1673 without additional quoting.
   1674 .P
   1675 All arithmetic evaluations are performed using
   1676 double precision floating point arithmetic.
   1677 Floating point constants follow the same rules as
   1678 the ANSI-C programming language.
   1679 Integer arithmetic constants are written as
   1680 .ce
   1681 .IB base # number,
   1682 where
   1683 .I base\^
   1684 is a decimal integer between
   1685 two and sixty-four and
   1686 .I number\^
   1687 is any non-negative number.
   1688 Base ten is used
   1689 when no base is specified.
   1690 The digits are represented by the characters
   1691 .BR 0-9a-zA-Z_@ .
   1692 For bases less than or equal to 36,
   1693 upper and lower case characters can
   1694 be used interchangeably to represent the digits
   1695 from 10 thru 35.
   1696 .P
   1697 Arithmetic expressions are made from constants,
   1698 variables, and operators.
   1699 Parentheses may be used for grouping.
   1700 The contents inside the double parentheses
   1701 are processed with the same expansions as occurs in a double quoted string,
   1702 so that all
   1703 .B $
   1704 expansions are performed before the expression is evaluated.
   1705 However, there is usually no need to use the
   1706 .B $
   1707 to get the value of a variable
   1708 because the arithmetic evaluator replaces the name of the variable
   1709 by its value within an arithmetic expression.
   1710 The
   1711 .B $
   1712 cannot be used when the variable is the subject of assignment
   1713 or an increment operation.
   1714 As a rule it is better not to use
   1715 .B $
   1716 in front of variables in an arithmetic expression.
   1717 .P
   1718 An arithmetic command of the form
   1719 .B
   1720 (( ... ))
   1721 .R
   1722 is a command that evaluates the enclosed arithmetic expression.
   1723 For example, the command
   1724 .ce
   1725 \f5(( x++ ))\fP
   1726 can be used to
   1727 increment the variable \f5x\fP,
   1728 assuming that \f5x\fP contains some numerical value.
   1729 The arithmetic command is true (return value 0), when the resulting
   1730 expression is non-zero, and false (return value 1) when the
   1731 expression evaluates to zero.
   1732 This makes the command easy to use with the \f5if\fP and \f5while\fP
   1733 compound commands.
   1734 .P
   1735 The \f5for\fP compound command
   1736 has been extended for use in arithmetic contexts. 
   1737 The syntax,
   1738 .ce
   1739 \f5for\fP \fB((\fP \fIexpr1\fP\fB;\fP \fIexpr2\fP \fB;\fP \fIexpr3 \fP\fB))\fP
   1740 can be used as the first line of a \f5for\fP loop with the same semantics
   1741 as the \f5for\fP statement in the ANSI-C programming language.
   1742 .P
   1743 Arithmetic evaluations can also be performed as part of the evaluation
   1744 of a command line.
   1745 The syntax
   1746 .B
   1747 $((\ ...\ ))
   1748 .R
   1749 expands to the value of the enclosed arithmetic expression.
   1750 This expansion can occur wherever parameter expansion is performed.
   1751 For example using the \f5ksh\fP command \f5print\fP (described
   1752 later)
   1753 .ce
   1754 \f5print $((2+2))\fP
   1755 prints the number 4.
   1756 .P
   1757 The following script prints the first
   1758 .I n
   1759 lines of its standard input onto its standard output,
   1760 where
   1761 .I n
   1762 can be supplied as an optional argument whose default value is 20.
   1763 .sp
   1764 .nf
   1765 .in .5i
   1766 .ta 4i
   1767 \f5integer n=${1-20}                       # set n
   1768 while   (( n-- >=0 )) && read -r line   # at most n lines
   1769 do      print -r -- "$line"
   1770 done\fP
   1771 .fi
   1772 .ta
   1773 .in
   1774 .sp
   1775 .H 2 "Shell Expansions"
   1776 .P
   1777 The commands you enter from the terminal or from a script
   1778 are divided into words and each word undergoes several
   1779 expansions to generate the command name and its arguments.
   1780 This is done in two phases.
   1781 The first phase recognizes reserved words, spaces and operators
   1782 to decide where command boundaries lie.
   1783 Alias substitutions take place during this phase.
   1784 The second phase performs expansions in the following order:
   1785 .BL
   1786 .LI
   1787 Tilde substitution,
   1788 parameter expansion,
   1789 arithmetic expansion,
   1790 and command substitution
   1791 are performed from left to right.
   1792 The option
   1793 .B \-u
   1794 or
   1795 .BR nounset ,
   1796 will cause an error to occur when any variable 
   1797 that is not set is expanded. 
   1798 .LI
   1799 The characters that result from parameter expansion and
   1800 command substitution above are checked with the characters
   1801 in the
   1802 \fB\s-1IFS\s+1\fP variable
   1803 for possible
   1804 field splitting.
   1805 (See a description of \f5read\fP below to see how
   1806 \fB\s-1IFS\s+1\fP is used.)
   1807 Setting
   1808 \fB\s-1IFS\s+1\fP to a null
   1809 value causes field splitting to be skipped.
   1810 .LI
   1811 Pathname generation (as described below)
   1812 is performed on each of the fields.
   1813 Any field that doesn't match a pathname is left alone.
   1814 The option,
   1815 .B \-f
   1816 or
   1817 .BR noglob ,
   1818 is used to disable pathname generation.
   1819 .LE
   1820 .H 2 "Pattern Matching"
   1821 The shell is primarily a string processing language and uses
   1822 patterns for matching file names as well as for matching strings.
   1823 The characters
   1824 .BR ? ,
   1825 .BR * ,
   1826 and
   1827 .B [
   1828 are processed specially 
   1829 by the shell when not quoted.
   1830 These characters are used to form patterns that
   1831 match strings.
   1832 Patterns are used by the shell to match pathnames,
   1833 to specify substrings,
   1834 and for
   1835 .B case
   1836 commands.
   1837 The character
   1838 .B ?
   1839 matches any one character.
   1840 The character
   1841 .B *
   1842 matches zero or more characters.
   1843 The character sequence
   1844 .BR [ ... ]
   1845 defines a character class
   1846 that matches any character contained within
   1847 .BR [\^] .
   1848 A range of characters can be specified by putting a
   1849 .B \-
   1850 between the first and last character of the range.
   1851 An exclamation mark,
   1852 .BR ! ,
   1853 immediately after the
   1854 .BR [ ,
   1855 means match all characters except the characters specified.
   1856 For example, the pattern
   1857 \f5a?c*.[!a-z]\fP
   1858 matches any string beginning with an
   1859 .BR a ,
   1860 whose third character is a
   1861 .BR c ,
   1862 and that ends in
   1863 .B .
   1864 (dot) followed by any character except the lower case letters,
   1865 .BR a\-z .
   1866 The sequence \f5[:alpha:]\fP
   1867 inside a character class, matches any set of characters in
   1868 the ANSI-C
   1869 .B alpha
   1870 class.
   1871 Similarly, \f5[:\fP\fIclass\fP\f5:]\fP matches
   1872 each of the characters in the given \fIclass\fP
   1873 for all the ANSI-C character classes.
   1874 For example, \f5[[:alnum:]_]\fP
   1875 matches any alpha-numeric character or the character
   1876 .BR _ .
   1877 .P
   1878 \f5ksh\fP
   1879 treats
   1880 strings of the form
   1881 .BI ( pattern-list
   1882 .BR ) ,
   1883 where
   1884 .I pattern-list
   1885 is a list of one or more patterns separated by a
   1886 .BR \(bv ,
   1887 specially when preceded by
   1888 .BR * ,
   1889 .BR ? ,
   1890 .BR + ,
   1891 .BR @ ,
   1892 or
   1893 .BR ! .
   1894 A
   1895 .B ?
   1896 preceding
   1897 .BI ( pattern-list )
   1898 means that the pattern list enclosed in
   1899 .B (\^)
   1900 is optional.
   1901 An
   1902 .BI @( pattern-list )
   1903 matches any pattern in the list of patterns enclosed in
   1904 .BR () .
   1905 A
   1906 .BI *( pattern-list )
   1907 matches any string that contains zero or more of each of the enclosed
   1908 patterns,
   1909 whereas
   1910 .BI +( pattern-list )
   1911 requires a match of one or more of any of the given patterns.
   1912 For instance, the pattern
   1913 .B +([0\-9])?(.)
   1914 matches one or more digits optionally followed by a
   1915 .BR . (dot).
   1916 A
   1917 .BI !( pattern-list )
   1918 matches anything except any of the given patterns.
   1919 For example,
   1920 \f5print\ !(*.o)\fP
   1921 displays all file names in the current directory that do not end in
   1922 .BR .o .
   1923 .P
   1924 When patterns are used to generate pathnames when expanding
   1925 commands several other rules apply.
   1926 A separate match is made
   1927 for each file name component of the pathname. 
   1928 Read permission is required for
   1929 any portion of the pathname that contains any special
   1930 pattern character. 
   1931 Search permission is required for every component except
   1932 possibly the last.
   1933 .P
   1934 By default,
   1935 file names in each directory that begin with \fB\s+2.\s-2\fP
   1936 are skipped when performing a match.
   1937 If the pattern to be matched starts with a leading \fB\s+2.\s-2\fP,
   1938 then only files beginning with a \fB\s+2.\s-2\fP,
   1939 are examined when reading each directory to find matching files.
   1940 If the
   1941 \fB\s-1FIGNORE\s+1\fP variable
   1942 is set,
   1943 then only files that do not match this pattern
   1944 are considered.
   1945 This overrides the special meaning of \fB\s+2.\s-2\fP
   1946 in a pattern and in a file name.
   1947 .P
   1948 If the
   1949 .B markdirs
   1950 option is set,
   1951 each matching pathname that is the name
   1952 of a directory has a trailing 
   1953 .B /
   1954 appended to the name.
   1955 .P
   1956 .H 2 "Conditional Expressions"
   1957 The Bourne shell uses the \f5test\fP
   1958 command, or the equivalent \f5[\fP
   1959 command, to test files for attributes
   1960 and to compare strings or numbers.
   1961 The problem with \f5test\fP
   1962 is that the shell has expanded the words of the \f5test\fP
   1963 command and
   1964 split them into arguments before \f5test\fP begins execution.
   1965 \f5test\fP
   1966 cannot distinguish between operators and operands.
   1967 In most cases
   1968 \f5test\ "$1"\fP
   1969 will test whether argument 1 is non-null.
   1970 However,
   1971 if argument 1 is
   1972 .BR \-f ,
   1973 then \f5test\fP
   1974 will treat
   1975 .B \-f
   1976 as an operator and
   1977 yield a syntax error.
   1978 One of the most frequent errors with
   1979 \f5test\fP
   1980 occurs when its operands are not within double quotes.
   1981 In this case, the argument may expand to more than a single
   1982 argument or to no argument at all.  In either case this
   1983 will likely cause a syntax error.
   1984 What makes this most insidious is that these errors are frequently
   1985 data dependent.  A script that appears to run correctly may abort
   1986 if given unexpected data.
   1987 .P
   1988 To get around these problems,
   1989 \f5ksh\fP
   1990 has a compound command for conditional expression testing
   1991 as part of the language.
   1992 The reserved words
   1993 .B [[
   1994 and
   1995 .B ]]
   1996 delimit the range of the command. 
   1997 Because they are reserved words, not operator characters,
   1998 they require spaces to separate them
   1999 from arguments.
   2000 The words between
   2001 .B [[
   2002 and
   2003 .B ]]
   2004 are not processed for field splitting or for pathname generation.
   2005 In addition, since \f5ksh\fP
   2006 determines the operators before parameter expansion,
   2007 expansions that yield no argument cause no problem.
   2008 The operators within
   2009 .BR [[ ... ]]
   2010 are almost the same as those for the \f5test\fP
   2011 command.
   2012 All unary operators are of the form
   2013 .BI \- letter
   2014 and are followed by a single operand.
   2015 Instead of
   2016 .B \-a
   2017 and
   2018 .BR \-o ,
   2019 .BR [[ ... ]]
   2020 uses
   2021 .B &&
   2022 and
   2023 .B \(bv\(bv
   2024 to indicate "and" and "or".
   2025 Parentheses are used without quoting for grouping.
   2026 .P
   2027 The right hand side of the string comparison operators
   2028 .B ==
   2029 and
   2030 .B !=
   2031 takes a pattern and tests whether the left hand operand
   2032 matches this pattern.  Quoting the pattern results
   2033 is a string comparison rather than the pattern match. 
   2034 The operators
   2035 .B <
   2036 and
   2037 .B >
   2038 within
   2039 .BR [[ ... ]]
   2040 designate lexicographical comparison.
   2041 .P
   2042 In addition there are several other new comparison primitives.
   2043 The binary operators
   2044 .B \-ot
   2045 and
   2046 .B \-nt
   2047 compare the modification times
   2048 of two files to see which file is
   2049 .I "older than"
   2050 or
   2051 .I "newer than"
   2052 the other.
   2053 The binary operator
   2054 .B \-ef
   2055 tests whether two files
   2056 have the same device and i-node number,
   2057 i.\ e., a link to the same file.
   2058 .P
   2059 The unary operator
   2060 .B \-L
   2061 returns true if its operand is a symbolic link.
   2062 The unary operator
   2063 .B \-O
   2064 (\fB\-G\fP)
   2065 returns true if the owner (or group) of the file operand matches
   2066 that of the caller.
   2067 The unary operator
   2068 .B \-o
   2069 returns true when its operand is the name of an option that is
   2070 currently on.
   2071 .P
   2072 The following script illustrates some of the uses of
   2073 .BR [[ ... ]] . 
   2074 The reference manual contains the complete list of operators.
   2075 .sp
   2076 .nf
   2077 .in .5i
   2078 .ta 4i
   2079 \f5for i
   2080 do      # execute foo for numeric directory
   2081         if      [[ \-d $i && $i == +([0\-9]) ]]
   2082         then    foo
   2083         # otherwise if writable or executable file and not mine
   2084         elif    [[ (\-w $i\(bv\(bv\-x $i) && ! \-O $i ]]
   2085         then    bar
   2086         fi
   2087 done\fP
   2088 .fi
   2089 .ta
   2090 .in
   2091 .sp
   2092 .H 2 "Input and Output"
   2093 \f5ksh\fP has
   2094 extended I/O capabilities to enhance the
   2095 use of the shell as a programming language.
   2096 As with the Bourne shell,
   2097 you use the I/O redirection operator,
   2098 .BR < ,
   2099 to control where input comes from,
   2100 and the I/O redirection operator,
   2101 .BR > ,
   2102 to control where output goes to. 
   2103 Each of these operators can be preceded with a single digit that
   2104 specifies a file unit number to associate with the file stream.
   2105 Ordinarily you specify these I/O redirection operators with a specific
   2106 command to which it applies.
   2107 However, if you specify I/O redirections with the \f5exec\fP
   2108 command,
   2109 and don't specify arguments to \f5exec\fP,
   2110 then the I/O redirection applies to the current program.
   2111 For example, the command
   2112 \f5exec\ <\ foobar\fP
   2113 opens file \f5foobar\fP
   2114 for reading.
   2115 The \f5exec\fP
   2116 command is also used to close files.
   2117 A file descriptor unit can be opened as a copy of an existing
   2118 file descriptor unit by using either of the
   2119 .B <&
   2120 or
   2121 .B >&
   2122 operators and putting the file descriptor unit of the original file
   2123 after the
   2124 .BR & .
   2125 Thus, \f52>&1\fP means open standard error (file descriptor 2)
   2126 as a copy of standard output (file descriptor 1).
   2127 A file descriptor value of
   2128 .B \-
   2129 after the
   2130 .B &
   2131 indicates that the file should be closed.
   2132 To close file unit 5, specify
   2133 \f5exec\ 5<&-\fP.
   2134 There are two additional redirection operators with \f5ksh\fP
   2135 and the POSIX shell that are not part of the Bourne shell.
   2136 The
   2137 .B >|
   2138 operator overrides the effect of the
   2139 .B noclobber
   2140 option described earlier.
   2141 The
   2142 .B <\^>
   2143 operator causes a file to be opened for both reading and writing.
   2144 .P
   2145 \f5ksh\fP recognizes certain pathnames and treats them
   2146 specially.
   2147 Pathnames of the form
   2148 .BI /dev/fd/ n\^
   2149 are treated as equivalent to the file defined by file descriptor
   2150 .IR n .
   2151 These name can be used as the script argument to \f5ksh\fP
   2152 and in conditional testing as described above.
   2153 On underlying systems that support
   2154 .B /dev/fd
   2155 in the file system, these names can be passed to other commands.
   2156 Pathnames of the form
   2157 .BI /dev/tcp/ hostid / port
   2158 and
   2159 .BI /dev/udp/ hostid / port
   2160 can be used to create
   2161 .B tcp
   2162 and
   2163 .B udp
   2164 connections to services given by the
   2165 .I hostid\^
   2166 number and
   2167 .I port\^
   2168 number.
   2169 The
   2170 .I hostid\^
   2171 cannot use symbolic values. In practice these
   2172 numbers are typically generated by command substitution.
   2173 For example,
   2174 \f5exec\ 5>\ /dev/tcp/$(service\ name)\fP
   2175 would open file descriptor 5 for sending messages
   2176 to hostid and port number defined by the output of \f5service\ name\fP.
   2177 .P
   2178 The Bourne shell has a built-in command \f5read\fP
   2179 for reading lines from standard input (file descriptor 0)
   2180 and splitting it into fields based on the value of the
   2181 .B \s-1IFS\s+1
   2182 variable, and a command \f5echo\fP
   2183 to write strings to standard output.
   2184 (On some systems, \f5echo\fP
   2185 is not a built-in command and incurs considerable overhead to use.)
   2186 Unfortunately, neither of these commands
   2187 is able to perform some very basic tasks.
   2188 For example.
   2189 with the Bourne shell,
   2190 the \f5read\fP
   2191 built-in cannot read a single line that ends in
   2192 .BR \e .
   2193 With \f5ksh\fP
   2194 the \f5read\fP
   2195 built-in has a
   2196 .B \-r
   2197 option to remove the special meaning for
   2198 .B \e
   2199 which allows it to be
   2200 treated as a regular
   2201 character rather than the line continuation character.
   2202 With the Bourne shell,
   2203 there is no simple way to have more than one file open
   2204 at any time for reading.
   2205 \f5ksh\fP has options on the \f5read\fP
   2206 command to specify the file
   2207 descriptor for the input.
   2208 The fields that are read from a line can be stored into an indexed
   2209 array with the
   2210 .B \-A
   2211 option to read.
   2212 This allows a line to be split into an arbitrary number of fields.
   2213 .P
   2214 The way the Bourne shell uses the
   2215 \fB\s-1IFS\s+1\fP variable to
   2216 split lines into fields greatly limits its utility.
   2217 Often data files consist of lines that use a character such
   2218 as
   2219 .B :
   2220 to delimit fields with two adjacent delimiters that denote
   2221 a null field.
   2222 The Bourne shell treats adjacent delimiters as a single
   2223 field delimiter.
   2224 With \f5ksh\fP,
   2225 delimiters that are considered white space characters
   2226 have the behavior of the Bourne shell, but other
   2227 adjacent delimiters separate
   2228 null fields.
   2229 .P
   2230 The \f5read\fP command is often used in scripts that interact
   2231 with the user by prompting the user and then requesting some
   2232 input.
   2233 With the Bourne shell two commands are needed; one to
   2234 prompt the user, the other to read the reply.
   2235 \f5ksh\fP allows these two commands to be combined.
   2236 The first argument of the \f5read\fP
   2237 command can be followed by a
   2238 .B ?
   2239 and a prompt string which is used whenever the input
   2240 device is a terminal.
   2241 Because the prompt is associated with the \f5read\fP built-in,
   2242 the built-in command line editors will be able to re-output
   2243 the prompt whenever the line needs to be refreshed when
   2244 reading from a terminal device.
   2245 .P
   2246 With the Bourne shell,
   2247 there is no way to set a time limit for waiting for the user
   2248 response to read.
   2249 The
   2250 .B \-t
   2251 option to \f5read\fP takes a floating
   2252 point argument that gives the time in seconds,
   2253 or fractions of seconds that the shell should wait for a reply.
   2254 .P
   2255 The version of the \f5echo\fP command in System V
   2256 treats certain sequences beginning with
   2257 .B \e
   2258 as control sequences.
   2259 This makes it hard to output strings without interpretation.
   2260 Most BSD derived systems do not interpret
   2261 .B \e
   2262 control sequences.
   2263 Unfortunately, the BSD versions of \f5echo\fP accepts a 
   2264 .B \-n
   2265 option to prevent a trailing new-line, but has no way to
   2266 cause the string
   2267 .B \-n
   2268 to be printed.
   2269 Neither of these versions is adequate. Also, because they
   2270 are incompatible, it is very hard to write portable shell scripts
   2271 using \f5echo\fP.
   2272 The \f5ksh\fP built-in, \f5print\fP,
   2273 outputs characters to the terminal or to a file and
   2274 subsumes the functions of all versions of \f5echo\fP.
   2275 Ordinarily, escape sequences in arguments beginning with
   2276 .B \e
   2277 are processed the same as for the System V \f5echo\fP command.
   2278 However \f5print\fP follows the standard conventions for
   2279 options and has options that make \f5print\fP very versatile.
   2280 The
   2281 .B \-r
   2282 option can be used to output the arguments without any special meaning.
   2283 The
   2284 .B \-n
   2285 option can be used here to suppress the trailing new-line
   2286 that is ordinarily appended.
   2287 As with \f5read\fP, it is possible to specify the file descriptor number
   2288 as an option to the command to avoid having to use
   2289 redirection operators with each occurrence of the command.
   2290 .P
   2291 The IEEE POSIX shell and utilities standard committee was unable
   2292 to reconcile the differences between the System V and BSD
   2293 versions of \f5echo\fP.
   2294 They introduced a new command named \f5printf\fP
   2295 which takes an ANSI-C format string and a list of options
   2296 and outputs the strings using the ANSI-C formatting rules.
   2297 Since \f5ksh\fP is POSIX conforming, it accepts \f5printf\fP.
   2298 However, there is a
   2299 .B \-f
   2300 options to \f5print\fP that can be used to specify
   2301 a format string which processes the arguments the same way that
   2302 \f5printf\fP does.
   2303 .P
   2304 The format processing for \f5print\fP and \f5printf\fP has
   2305 been extended slightly.
   2306 There are three additional formatting directives.
   2307 The
   2308 .B %b
   2309 format causes the
   2310 .B \e
   2311 escape sequences to be expanded as they are with the System V \f5echo\fP
   2312 command.
   2313 The
   2314 .B %q
   2315 format causes quotes to
   2316 be placed on the output as required
   2317 so that it can be used as shell input.
   2318 Special characters in the output of most \f5ksh\fP built-in commands
   2319 and in the output from an execution trace
   2320 are quoted in an equivalent fashion. 
   2321 The
   2322 .B %P
   2323 format causes an extended regular expression string to
   2324 be converted into a shell pattern.
   2325 This is useful for writing shell applications that have
   2326 to accept regular expressions as input.
   2327 Finally, the escape sequence
   2328 .B \e\^E
   2329 which expands to the terminal escape character (octal 033)
   2330 has been added.
   2331 .P
   2332 The shell is frequently used as a programming language for
   2333 interactive dialogues.
   2334 The
   2335 \f5select\fP
   2336 statement has been added to the language
   2337 to make it easier to
   2338 present menu selection alternatives to the
   2339 user and evaluate the reply.
   2340 The list of alternatives is numbered and put in columns.
   2341 A user settable prompt,
   2342 \fB\s-1PS3\s+1\fP,
   2343 is issued and if the answer is
   2344 a number corresponding to one of the alternatives,
   2345 the select loop variable is set to this value.
   2346 In any case, the
   2347 .B \s-1REPLY\s+1
   2348 variable is used to store the user entered reply.
   2349 The shell variables
   2350 .B \s-1LINES\s+1
   2351 and
   2352 .B \s-1COLUMNS\s+1
   2353 are used to control the layout of select lists.
   2354 .H 2 "Option Parsing"
   2355 The \f5getopts\fP built-in command can be used
   2356 to process command arguments in a manner consistent
   2357 with the way \f5ksh\fP does for its own built-in commands.
   2358 .P
   2359 The \f5getopts\fP built-in allows users to specify options
   2360 as separate arguments or to group options that do not
   2361 take arguments together.  Options that require arguments
   2362 do not require space to separate them from the option argument.
   2363 The
   2364 .B \s-1OPTARG\s+1
   2365 variable stores the value of the option argument
   2366 after finding a variable that takes an argument.
   2367 The
   2368 .B \s-1OPTIND\s+1
   2369 variable holds the index of the current options argument.
   2370 After processing options, the arguments should be
   2371 shifted by
   2372 .B \s-1OPTIND\s+1\-1
   2373 to make the
   2374 remaining arguments be \f5"$@"\fP.
   2375 .P
   2376 The \f5getopts\fP argument description allows additional
   2377 information to be specified along with the options
   2378 that is used to generate \fIusage\fP messages for
   2379 incorrect arguments and for the option argument \fB\-?\fP.
   2380 The example in the APPENDIX uses \f5getopts\fP to process
   2381 its arguments.
   2382 .H 2 "Co-process"
   2383 \f5ksh\fP can spawn a
   2384 .I co-process
   2385 by adding a
   2386 .B "|&"
   2387 after a command.
   2388 This process will be run with its standard input and its
   2389 standard output connected to the shell.  The built-in command \f5print\fP
   2390 with the
   2391 .B \-p
   2392 option will write into the standard input of this
   2393 process and
   2394 the built-in command \f5read\fP
   2395 with the
   2396 .B \-p
   2397 option will read from the output of this process.
   2398 .P
   2399 In addition, the I/O redirection operators \fB<&\fP and \fB>&\fP can
   2400 be used to move the input or output pipe of the co-process
   2401 to a numbered file descriptor.
   2402 Use \f5exec\ 3>&\ p\fP to move the input of the co-process
   2403 to file descriptor \fB3\fP.
   2404 After you have connected to file descriptor \fB3\fP, you
   2405 can direct the output of any command to the co-process
   2406 by running \fIcommand\fP\f5\ >&3\fP.
   2407 Also, by moving the input of the co-process to a numbered descriptor,
   2408 it is possible to run a second co-process.
   2409 The output of both co-processes will be the file descriptor
   2410 associated with \f5read\ -p\fP.
   2411 You can use \f5exec\ 4<&\ p\fP to cause the output of these
   2412 co-processes to go to file descriptor \fB4\fP of the shell.
   2413 Once you have moved the pipe to descriptor \fB4\fP, it is possible
   2414 to connect a server to the co-process by running \fIcommand\fP\f5\ 4<&\ p\fP
   2415 or to close the co-process pipe with \f5exec\ 4<&\ -\fP.
   2416 .H 2 "Functions"
   2417 .P
   2418 Function definitions are of the form
   2419 .sp
   2420 .in +.5i
   2421 .nf
   2422 \f5function\fP \fIname\fP
   2423 .br
   2424 .B {
   2425 .br
   2426         any shell script
   2427 .br
   2428 .B }
   2429 .fi
   2430 .sp
   2431 .in
   2432 A function whose name contains a \fB\s+2.\s-2\fP
   2433 is called a \fIdiscipline\fP function.
   2434 The portion of the name after the last \fB\s+2.\s-2\fP
   2435 is the name of the discipline.
   2436 Discipline functions named \f5get\fP, \f5set\fP, and \f5unset\fP
   2437 can be assigned to any variable to intercept lookups,
   2438 assignments and unsetting of the variable
   2439 defined by the portion of the name before the last \fB\s+2.\s-2\fP.
   2440 Applications can create additional disciplines for variables
   2441 that are created as part of user defined built-ins.
   2442 The portion of the name before the last \fB\s+2.\s-2\fP
   2443 must refer to the name of an existing variable.
   2444 Thus, if \f5p\fP is a reference to \f5PATH\fP, then
   2445 the function name \f5p.get\fP and \f5PATH.get\fP
   2446 refer to the same function.
   2447 .P
   2448 The function is invoked either
   2449 by specifying
   2450 .I name
   2451 as the command name
   2452 and optionally following it with arguments
   2453 or by using it as an option to the \fB\s+2.\s-2\fP
   2454 built-in command.
   2455 Positional parameters are saved before each
   2456 function call and restored when completed.
   2457 The arguments that follow the function name on the calling
   2458 line become positional parameters inside the function.
   2459 The \f5return\fP
   2460 built-in can be used to cause the function to return to
   2461 the statement following
   2462 the point of invocation.
   2463 .P
   2464 Functions can also be defined with the System V notation,
   2465 .sp
   2466 .in +.5i
   2467 .nf
   2468 \fIname\fP \f5()\fP
   2469 .br
   2470 .B {
   2471 .br
   2472         any shell script
   2473 .br
   2474 .B }
   2475 .fi
   2476 .sp
   2477 .in
   2478 Functions defined with this syntax cannot be used as the first
   2479 argument to a \fB\s+2.\s-2\fP procedure.
   2480 \f5ksh\fP accepts this notation for compatibility only.
   2481 There is no need to use this notation when writing
   2482 \f5ksh\fP scripts.
   2483 .P
   2484 Functions defined with the \f5function\fP\ \fIname\fP syntax
   2485 and invoked by name
   2486 are executed in the current shell environment
   2487 and can share named variables with the calling program.
   2488 Options, other than execution trace
   2489 .BR \-x ,
   2490 set by the calling program are
   2491 passed down to a function.
   2492 The options are
   2493 not shared with
   2494 the function so that any options set within a function are
   2495 restored when the function exits.
   2496 Traps ignored by the caller are ignored within the function
   2497 and cannot be enabled.
   2498 Traps caught by the calling program are reset to their
   2499 default action within the function.
   2500 In most instances, the default action is
   2501 to cause the function to terminate.
   2502 A trap on
   2503 \fB\s-1EXIT\s+1\fP
   2504 defined within a function executes after the function
   2505 completes but
   2506 before the caller resumes.
   2507 Therefore,
   2508 any variable assignments and
   2509 any options set as part of a trap action will be effective
   2510 after the caller resumes.
   2511 .P
   2512 By default, variables are inherited by the function and shared
   2513 by the calling program.
   2514 However,
   2515 for functions defined with the \f5function\fP\ \fIname\fP syntax
   2516 that are invoked by name,
   2517 environment substitutions preceding the function call
   2518 apply only to the scope of the function call.
   2519 Also, variables whose names do not contain a \fB\s+2.\s-2\fP
   2520 that are defined with the \f5typeset\fP
   2521 built-in command are local to the function that they are declared in.
   2522 Thus, for the function defined
   2523 .sp
   2524 .nf
   2525 .in .5i
   2526 \f5function  name
   2527 {
   2528      typeset -i x=10
   2529      let z=x+y
   2530      print $z
   2531 }\fP
   2532 .fi
   2533 .ta
   2534 .in
   2535 .sp
   2536 invoked as
   2537 \f5y=13\ name\fP,
   2538 \f5x\fP and \f5y\fP
   2539 are local variables with respect to the function
   2540 \f5name\fP
   2541 while
   2542 \f5z\fP
   2543 is global.
   2544 .P
   2545 Functions defined with the \fIname\fP\f5()\fP syntax,
   2546 and functions invoked as an argument to the \fB\s+2.\s-2\fP
   2547 command,
   2548 share everything other than positional parameters with the caller.
   2549 Assignments that precede the call remain in effect after the
   2550 function completes.
   2551 .P
   2552 Alias and function names are not passed down to shell scripts
   2553 or carried across separate
   2554 invocations of \f5ksh\fP.
   2555 The
   2556 .B $\s-1FPATH\s+1
   2557 variable gives a colon separated list of directories that
   2558 is searched for function definitions when trying to resolve
   2559 the command name.
   2560 Whenever a file name contained in
   2561 .B $\s-1FPATH\s+1
   2562 is found, the complete file is read and all functions
   2563 contained within become defined.
   2564 .P
   2565 Calls that reference functions can be recursive.
   2566 Except for special built-ins,
   2567 function names take precedence over built-in names and names
   2568 of programs when used as command names.
   2569 To write a replacement function that invokes the command that
   2570 you wish to replace,
   2571 you can use the \f5command\fP built-in command.
   2572 The arguments to \f5command\fP are the name and arguments
   2573 of the program you want to execute.
   2574 For example to write a
   2575 .B cd
   2576 function which changes the directory and prints out the directory name,
   2577 you can write
   2578 .sp
   2579 .nf
   2580 .in .5i
   2581 \f5function  cd
   2582 {
   2583      if      command cd  "$@"
   2584      then    print  -r -- $PWD
   2585      fi
   2586 }\fP
   2587 .fi
   2588 .ta
   2589 .in
   2590 .sp
   2591 .P
   2592 The
   2593 \fB\s-1FPATH\s+1\fP
   2594 variable is a colon separated list that \f5ksh\fP
   2595 uses to search for function definitions.
   2596 When
   2597 \f5ksh\fP
   2598 encounters an autoload function,
   2599 it runs the
   2600 .B .
   2601 command on the script containing the function,
   2602 and then executes the function.
   2603 .P
   2604 For interactive shells,
   2605 function definitions may also be placed in the
   2606 \fB\s-1ENV\s+1\fP
   2607 file.
   2608 However, this
   2609 causes the shell to take longer to begin executing.
   2610 .H 2 "Process Substitution"
   2611 .P
   2612 This feature is only available
   2613 on versions of the UNIX operating system which support the
   2614 .B /dev/fd
   2615 directory for naming open files.
   2616 Each command argument of the form
   2617 \fB<(\fP\fIlist\^\fP\fB)\fP
   2618 or
   2619 \fB>(\fP\fIlist\^\fP\fB)\fP
   2620 will run process
   2621 .I list
   2622 asynchronously connected to some file in the
   2623 .B /dev/fd
   2624 directory.
   2625 The name of this file will become the argument to the command.
   2626 If the form with
   2627 .B >
   2628 is selected then writing on this file will provide input for
   2629 .IR list .
   2630 If
   2631 .B <
   2632 is used,
   2633 then the file passed as an argument will contain the output of the
   2634 .I list
   2635 process.
   2636 For example,
   2637 .sp
   2638 .nf
   2639 .in .5i
   2640 \f5paste  <(cut \-f1 \fP\fIfile1\fP\f5)  <(cut \-f2 \fP\fIfile2\fP\f5) | tee >(\fP\fIprocess1\fP\f5)  >(\fP\fIprocess2\fP\f5)\fP
   2641 .fi
   2642 .ta
   2643 .in
   2644 .sp
   2645 extracts
   2646 fields 1 and 3 from
   2647 the files
   2648 .I file1
   2649 and
   2650 .I file2
   2651 respectively,
   2652 places the
   2653 results side by side, and
   2654 sends it
   2655 to the processes
   2656 .I process1
   2657 and
   2658 .IR process2 ,
   2659 as well as putting it onto the standard output.
   2660 Note that the file which is passed as an argument to the command is
   2661 a UNIX system
   2662 .IR pipe (2)
   2663 so that the programs that expect to
   2664 .IR lseek (2)
   2665 on the file will not work.
   2666 .H 2 "Finding Commands"
   2667 .P
   2668 The addition of aliases, functions,
   2669 and more built-ins
   2670 has made it substantially more difficult to know what
   2671 a given command name really means.
   2672 .P
   2673 Commands that begin with reserved words
   2674 are an integral part of the shell language itself
   2675 and typically define the control flow of the language.
   2676 Some control flow commands are not reserved words in
   2677 the language but are \fIspecial\fP built-ins.
   2678 Special built-ins are built-ins that are considered a
   2679 part of the language rather than user definable commands.
   2680 The best examples of commands that fit this description
   2681 are \f5break\fP and \f5continue\fP.
   2682 Because they are not reserved words, they can be the
   2683 result of shell expansions and are not effected by quoting. 
   2684 These commands have the following special properties:
   2685 .BL
   2686 .LI
   2687 Assignments that precede them apply to the current shell process,
   2688 not just to the given command.
   2689 .LI
   2690 An error in the format of these commands cause a shell script
   2691 or function that contains them to abort.
   2692 .LI
   2693 They cannot be overridden by shell functions.
   2694 .LE
   2695 .P
   2696 Other commands are built-in because they perform side effects
   2697 on the current environment that would be nearly impossible
   2698 to implement otherwise.
   2699 Built-ins such as \f5cd\fP and \f5read\fP
   2700 are examples of such built-ins.
   2701 These built-ins are semantically equivalent to commands that
   2702 are not built-in except that they don't take a path search
   2703 to locate.
   2704 .P
   2705 A third reason to have a command built-in is so that
   2706 it will be unaffected by the setting of the
   2707 .B \s-1PATH\s+1
   2708 variable.
   2709 The \f5print\fP  command fits this category.
   2710 Scripts that use \f5print\fP will be portable
   2711 to all sites that run \f5ksh\fP.
   2712 .P
   2713 The final reason for having a command be a built-in is
   2714 for performance.
   2715 On most systems it is more than an order of magnitude
   2716 faster to initiate a command that is built-in than
   2717 to create a separate process to run the command.
   2718 Examples that fit this category are \f5test\fP
   2719 and \f5pwd\fP.
   2720 .P
   2721 Given a command name \f5ksh\fP decides what it means using
   2722 the following order:
   2723 .BL
   2724 .LI
   2725 Reserved words define commands that form part of the shell
   2726 grammar.
   2727 They cannot be quoted.
   2728 .LI
   2729 Alias substitutions occur first as part of the reading of commands.
   2730 Using quotes in the command name will prevent alias substitutions.
   2731 .LI
   2732 Special built-ins.
   2733 .LI
   2734 Functions.
   2735 .LI
   2736 Commands that are built-in that are not associated with a pathname
   2737 such as \f5cd\fP and \f5print\fP.
   2738 .LI
   2739 If the command name contains a
   2740 .BR / ,
   2741 the program or script corresponding to the given name is executed.
   2742 .LI
   2743 A path search locates the pathname corresponding to the command.
   2744 If the pathname where it is found matches the pathname associated
   2745 with a built-in command, the built-in command is executed.
   2746 If the directory where the command is found is listed in the
   2747 .B \s-1FPATH\s+1
   2748 variable, the file is read into the shell
   2749 like a dot script, and a function by that name is invoked.
   2750 Once a pathname is found, \f5ksh\fP remembers its location
   2751 and only checks relative directories in \fB\s-1PATH\s+1\fP
   2752 the next time the command name is used.
   2753 Assigning a value to \fB\s-1PATH\s+1\fP
   2754 causes \f5ksh\fP to forget the location of all command names.
   2755 .LI
   2756 The
   2757 .B \s-1FPATH\s+1
   2758 variable is searched and files found are treated as described above.
   2759 .LE
   2760 .P
   2761 The first argument of the \f5command\fP built-in, described earlier,
   2762 skips the checks for reserved words and for function definitions.
   2763 In all other ways, \f5command\fP behaves like a built-in
   2764 that is not associated with a pathname.
   2765 As a result, if the first argument of \f5command\fP is
   2766 a special built-in, the special properties of this built-in
   2767 do not apply.  
   2768 For example, whereas, \f5exec\ 3<\ foo\fP will cause a script containing
   2769 it to abort if the open fails, \f5command\ exec\ 3<\ foo\fP
   2770 results in a non-zero exit status but does not abort the script.
   2771 .P
   2772 You can get a complete list of the special built-in commands
   2773 with \f5builtin\ -s\fP. 
   2774 In addition \f5builtin\fP without arguments gives a list of
   2775 the current built-ins and the pathname that they are associated with.
   2776 A built-in can be bound to another pathname by giving
   2777 the pathname for the built-in.  The basename of this path must
   2778 be the name of an existing built-in for this to succeed.
   2779 Specifying the name of the built-in without a pathname causes
   2780 this built-in to be found before a path search.
   2781 A built-in can be deleted  with the \fB\-d\fP option.
   2782 .P
   2783 On systems with run time loading of libraries, built-in commands
   2784 can be added with the \f5builtin\fP command.
   2785 Each command that is to be built-in must be written as a
   2786 C function whose name is of the form \f5b_\fP\fIname\fP, where
   2787 \fIname\fP is the name of the built-in that is to be added.
   2788 The function has the same argument calling convention as
   2789 \f5main\fP.  The lower eight bits of the return value become
   2790 the exit status for this built-in.
   2791 Builtins are added by specifying the pathname of the library
   2792 as an argument to the \fB\-f\fP option of \f5builtin\fP.
   2793 .P
   2794 The built-in command,
   2795 \f5whence\fP,
   2796 when used with the
   2797 .B \-v
   2798 option, tells how a given command is bound.
   2799 A line is printed for each argument to \f5whence\fP
   2800 telling what would happen if this argument were used as a command name.
   2801 It reports on reserved words, aliases, built-ins, and
   2802 functions.
   2803 If the command is none of the above,
   2804 it follows the path search rules and prints the full path-name,
   2805 if any, otherwise it prints an error message.
   2806 .H 2 "Symbolic Names"
   2807 To avoid implementation dependencies, \f5ksh\fP
   2808 accepts and generates symbolic names
   2809 for built-ins that use numerical values in the Bourne shell.
   2810 The
   2811 .B \-S
   2812 option of the
   2813 \f5umask\fP built-in command
   2814 accepts and displays
   2815 default file creation permissions
   2816 symbolically.
   2817 It uses the same symbolic notation as the \f5chmod\fP command.
   2818 .P
   2819 The \f5trap\fP and \f5kill\fP built-in commands
   2820 allows the signal names to be given symbolically.
   2821 The names of signals and traps
   2822 corresponding to signals are the same as the signal name with
   2823 the
   2824 .B \s-1SIG\s+1
   2825 prefix removed.
   2826 The trap
   2827 .B 0
   2828 is named
   2829 \fB\s-1EXIT\s+1\fP.
   2830 .H 2 "Additional Variables"
   2831 In addition to the variables discussed earlier, \f5ksh\fP
   2832 has other variables that it handles specially. 
   2833 The variable \fB\s-1RANDOM\s+1\fP
   2834 produces a random number in the range 0 to 32767 each time it is referenced.
   2835 Assignment to this variable sets the seed for the
   2836 random number generator.
   2837 .P
   2838 The parameter \fB\s-1PPID\s+1\fP
   2839 is used to generate the process id of the process which invoked this shell.
   2840 .H 2 "Added Traps"
   2841 A new trap named
   2842 \fB\s-1ERR\s+1\fP
   2843 has been added.
   2844 This trap is invoked whenever the shell would exit if the
   2845 .B \-e
   2846 option were set.
   2847 This trap is used by
   2848 Fourth Generation Make\*(Rf
   2849 .RS
   2850 G. S. Fowler,
   2851 .I "The Fourth Generation Make,"
   2852 Proceedings of the Portland USENIX meeting, pp. 159-174, 1985.
   2853 .RF
   2854 which runs \f5ksh\fP
   2855 as a co-process.
   2856 .P
   2857 A trap named
   2858 \fB\s-1DEBUG\s+1\fP
   2859 gets executed after each command.
   2860 This trap can be used for debugging and other purposes.
   2861 .P
   2862 The
   2863 \fB\s-1KEYBD\s+1\fP
   2864 trap was described earlier.
   2865 .H 2 Debugging
   2866 The primary method for debugging Bourne shell scripts is to
   2867 use the
   2868 .B \-x
   2869 option to enable the execution trace.
   2870 After all
   2871 the expansions have been performed,
   2872 but before each command is executed,
   2873 the trace writes to standard error the name and arguments
   2874 of each command preceded by a
   2875 .BR + .
   2876 While the trace is very useful, there is no way
   2877 to find out what line of source a given trace line
   2878 corresponds to.
   2879 With
   2880 \f5ksh\fP
   2881 the
   2882 \fB\s-1PS4\s+1\fP
   2883 variable
   2884 is evaluated for parameter expansion and
   2885 is displayed before each command,
   2886 instead of the
   2887 .BR + .
   2888 .P
   2889 The
   2890 \fB\s-1LINENO\s+1\fP
   2891 variable is set to the current line number relative to the
   2892 beginning of the current script or function. 
   2893 It is most useful as part of the
   2894 \fB\s-1PS4\s+1\fP
   2895 prompt.
   2896 .P
   2897 The
   2898 \fB\s-1DEBUG\s+1\fP
   2899 trap can be used to write a break point shell
   2900 debugger in \f5ksh\fP.
   2901 An example of such a debugger is \f5kshdb\fP.\*(Rf
   2902 .RS
   2903 Bill Rosenblatt,
   2904 .IR "Debugging Shell Scripts with \f5kshdb\fP" ,
   2905 Unix World, Volume X, No. 5, 1993.
   2906 .RF
   2907 .H 2 "Timing Commands"
   2908 .P
   2909 Finding the time it takes to execute commands
   2910 has been a serious problem with the Bourne shell.
   2911 Since the \f5time\fP command is not part of the
   2912 language, it is necessary to write a script
   2913 in order to time a \f5for\fP or \f5while\fP loop.
   2914 The extra time in invoking the shell and processing
   2915 the script is accumulated along with the time
   2916 to execute the script.
   2917 .P
   2918 More seriously, the Bourne shell does not give correct
   2919 times for pipelines.
   2920 The reason for this is that the times for some members
   2921 of a pipeline are not counted when computing the time.
   2922 As an extreme example,
   2923 running \f5time\fP on the script
   2924 .ce
   2925 \f5cat < /dev/null | sort -u bigfile | wc\fP
   2926 with the Bourne shell will show very little
   2927 user and system time no matter how
   2928 large \f5bigfile\fP is.
   2929 .P
   2930 To correct these problems,
   2931 a reserved word \f5time\fP
   2932 has been added to replace
   2933 the \f5time\fP
   2934 command.
   2935 Any function, command or pipeline can be preceded by this reserved word
   2936 to obtain information about the elapsed, user, and system times.
   2937 Since I/O redirections bind to the command, not to
   2938 \f5time\fP,
   2939 parentheses should be used to redirect the timing information which
   2940 is normally printed on file descriptor 2.
   2941 .H 1 SECURITY
   2942 There are several documented problems associated with the security of
   2943 shell procedures\*(Rf.
   2944 .RS
   2945 F. T. Grampp and R. H. Morris,
   2946 .I "UNIX Operating System Security,"
   2947 AT&T Bell Labs Tech. Journal, Vol. 63, No. 8, Part 2, pp. 1649-1671, 1984.
   2948 .RF
   2949 These security holes occur primarily because a user can manipulate the
   2950 .I environment
   2951 to subvert the intent of a
   2952 .I setuid
   2953 shell procedure.
   2954 Sometimes, shell procedures are initiated from 
   2955 binary programs, without the author's
   2956 awareness, by library routines which invoke shells to carry out
   2957 their tasks.
   2958 When the binary program is run
   2959 .I setuid
   2960 then the shell procedure runs with the permissions afforded to the
   2961 owner of the binary file.
   2962 .P
   2963 In the Bourne shell,
   2964 the
   2965 .B \s-1IFS\s+1
   2966 parameter is used to split each word into separate command arguments.
   2967 If a user knows that some
   2968 .I setuid
   2969 program will run
   2970 \f5sh\ -c\ /bin/pwd\fP
   2971 (or any other command in
   2972 .BR /bin )
   2973 then the user sets and exports
   2974 .BR \s-1IFS\s+1=\^/ .
   2975 Instead of running
   2976 .B /bin/pwd
   2977 the shell will run
   2978 .B bin
   2979 with
   2980 .B pwd
   2981 as an argument.
   2982 The user puts his or her own \f5bin\fP
   2983 program into the current directory.
   2984 This program can
   2985 create a copy of the shell,
   2986 make this shell
   2987 .IR setuid ,
   2988 and then run the \f5/bin/pwd\fP
   2989 program so that the original program continues to run successfully.
   2990 This kind of penetration is not possible with
   2991 \f5ksh\fP
   2992 since the
   2993 .B \s-1IFS\s+1
   2994 parameter only splits arguments that result from command or parameter
   2995 substitution.
   2996 .P
   2997 Some
   2998 .I setuid
   2999 programs run programs using
   3000 .I system()
   3001 without giving the full pathname.
   3002 If the
   3003 user sets the
   3004 .B \s-1PATH\s+1
   3005 variable so that the desired command will be found
   3006 in his or her local bin, then the same technique described above can
   3007 be employed to compromise the security of the system.
   3008 To close up this and other security holes,
   3009 \f5ksh\fP
   3010 resets the effective user id to the real user id and the effective 
   3011 group id to the real group id unless the
   3012 .I privileged
   3013 option
   3014 .RB ( \-p\^ )
   3015 is specified at invocation.
   3016 In
   3017 this mode, the
   3018 .B privileged
   3019 mode, the
   3020 .B .profile
   3021 and
   3022 .B \s-1ENV\s+1
   3023 files are not processed.
   3024 Instead, the file
   3025 .B /etc/suid_profile
   3026 is read and executed.
   3027 This gives an administrator control over the
   3028 environment to set the
   3029 .B \s-1PATH\s+1
   3030 variable or to log setuid shell invocations.
   3031 Clearly security of the system is compromised if
   3032 .B /etc
   3033 or this file is publicly writable.
   3034 .P
   3035 Some versions of the UNIX operating system look for the characters
   3036 \f5#!\fP
   3037 as the first two characters of an executable file.
   3038 If these characters are found, then the next word on this line is taken
   3039 as the interpreter to
   3040 invoke
   3041 for this command and the interpreter is
   3042 .IR exec ed
   3043 with the name of the script as argument zero and argument one.
   3044 If the
   3045 .I setuid
   3046 or
   3047 .I setgid
   3048 bits are on for this file, then the interpreter
   3049 is run with the effective uid and/or gid set accordingly.
   3050 This scheme has three major drawbacks.
   3051 First of all,
   3052 putting the pathname of the interpreter into the script
   3053 makes the script less portable since the interpreter
   3054 may be installed in a different directory on another system.
   3055 Secondly, using the
   3056 \f5#!\fP
   3057 notation forces an
   3058 .B exec
   3059 of the interpreter even when the call is invoked from the interpreter
   3060 which it must exec.  This is inefficient since
   3061 \f5ksh\fP can handle a failed exec much faster than starting up
   3062 again.
   3063 More importantly,
   3064 .I setuid
   3065 and
   3066 .I setgid
   3067 procedures provide an easy target for intrusion.
   3068 By linking a
   3069 .I setuid
   3070 or
   3071 .I setgid
   3072 procedure to a name beginning with a
   3073 .B \-
   3074 the interpreter is fooled into thinking that it is being invoked with
   3075 a command line option rather than the name of a file.
   3076 When the interpreter is the shell, the user gets a privileged
   3077 interactive shell.
   3078 There is code in
   3079 \f5ksh\fP
   3080 to guard against this simple form of intrusion.
   3081 .P
   3082 A more reliable way to handle
   3083 .I setuid
   3084 and
   3085 .I setgid
   3086 procedures is provided with
   3087 \f5ksh\fP.
   3088 The technique does not require any changes to the operating system
   3089 and provides better security.
   3090 Another advantage to this method is that it also allows scripts which
   3091 have execute permission but no read permission to run.  Taking away read
   3092 permission makes scripts more secure.
   3093 .P
   3094 The method relies on a setuid
   3095 .B root
   3096 program to authenticate the
   3097 request and exec the shell with the correct mode bits to carry out
   3098 the task.  This shell is invoked with the requested file already open
   3099 for reading.  A script which cannot be opened for reading or which
   3100 has its setuid and/or setgid bits turned on causes this setuid
   3101 .B root
   3102 program to get \fBexec\fPed.
   3103 For security reasons, this program is given the full
   3104 pathname
   3105 \f5/etc/suid_exec\fP.
   3106 A description of the implementation of the
   3107 \f5/etc/suid_exec\fP
   3108 program can be found in
   3109 a separate paper\*(Rf.
   3110 .RS
   3111 D. G Korn
   3112 .I "Parlez-vous Kanji?"
   3113 TM-59554-860602-03, 1986.
   3114 .RF
   3115 .H 1 "CODE CHANGES"
   3116 \f5ksh\fP is written in ANSI-C as a reusable library.
   3117 The code can be compiled with C++ and older K&R C as well.
   3118 The code uses the IEEE POSIX 1003.1 and ISO 9945-1 standard\*(Rf
   3119 .RS
   3120 .I "POSIX \- Part 1: System Application Program Interface,"
   3121 IEEE Std 1003.1-1990, ISO/IEC 9945-1:1990.
   3122 .RF
   3123 wherever possible so that \f5ksh\fP should be able to run
   3124 on any POSIX compliant system.  In addition, it is possible
   3125 to compile \f5ksh\fP for older systems.
   3126 .P
   3127 Unlike earlier version of the Bourne shell,
   3128 \f5ksh\fP treats eight bit characters transparently
   3129 without stripping off the
   3130 leading bit.
   3131 There is also a compile time switch to enable handling multi-byte
   3132 and multi-width characters sets.
   3133 .P
   3134 On systems with dynamic libraries, it is possible to add built-in
   3135 commands at run time  with the built-in command \f5builtin\fP 
   3136 described earlier.
   3137 It is also possible to embed \f5ksh\fP in applications in
   3138 a manner analogous to \f5tcl\fP.
   3139 .H 1 "EXAMPLE"
   3140 .P
   3141 An example of a \f5ksh\fP script is included
   3142 in the Appendix.
   3143 This one page program is a variant of the UNIX system
   3144 \f5grep\fP(1) program.
   3145 Pattern matching for this version of \f5grep\fP
   3146 means shell patterns.
   3147 .P
   3148 The first half uses the \f5getopts\fP command to
   3149 find the option flags.
   3150 Nearly all options have been implemented.
   3151 The second half goes through each line of each file
   3152 to look for a pattern match.
   3153 .P
   3154 This program is not intended to serve as a
   3155 replacement for \f5grep\fP
   3156 which has been highly tuned for performance.
   3157 It does
   3158 illustrate the programming power of \f5ksh\fP.
   3159 Note that no auxiliary processes are spawned by this script.
   3160 It was written and debugged in under two hours.
   3161 While performance is acceptable for small files,
   3162 this program runs at only one tenth
   3163 the speed of \f5grep\fP
   3164 for large files.
   3165 .H 1 "PERFORMANCE"
   3166 .P
   3167 \f5ksh\fP executes many scripts faster than the System V Bourne shell;
   3168 in some cases more than 10 times as fast.
   3169 The primary reason for this is that \f5ksh\fP creates fewer
   3170 processes.
   3171 The time to execute a built-in command or a function is one or two
   3172 orders of magnitude faster than performing a \f5fork\fP() and
   3173 \f5exec\fP() to create a separate process.
   3174 Command substitution and commands inside parentheses
   3175 are performed without creating another process, unless necessary
   3176 to preserve correct behavior.
   3177 .P
   3178 Another reason for improved performance is the use of the \fBsfio\fP\*(Rf,
   3179 .RS
   3180 David Korn and Kiem-Phong Vo,
   3181 .IR "SFIO - A Safe/Fast String/File I/O,"
   3182 Proceedings of the Summer Usenix,
   3183 pp. 235-255, 1991.
   3184 .RF
   3185 library for I/O.  The \fBsfio\fP library buffers all I/O
   3186 and buffers are flushed only when required.
   3187 The algorithms used in \fBsfio\fP perform better than
   3188 traditional versions of standard I/O so that programs that
   3189 spend most of their time
   3190 formatting output may actually perform better
   3191 than versions written in C.
   3192 .P
   3193 Several of the internal algorithms have been changed
   3194 so that the number of subroutine calls has been
   3195 substantially reduced.
   3196 \f5ksh\fP uses variable sized hash tables for variables.
   3197 Scripts that rely heavily on referencing variables execute faster.
   3198 More processing is performed while reading the script
   3199 so that execution time is saved while running loops.
   3200 These changes are not noticeable for scripts that \f5fork()\fP
   3201 and run processes,
   3202 but they reduce the time that it takes to interpret commands by
   3203 more than a factor of two.
   3204 .P
   3205 Most importantly, \f5ksh\fP provide mechanisms to write applications
   3206 that do not require as many processes.
   3207 The arithmetic provided by the shell eliminates the need for the
   3208 \f5expr\fP command.
   3209 The pattern matching and substring capabilities eliminate the
   3210 need to use \f5sed\fP or \f5awk\fP to process strings.
   3211 .P
   3212 The architecture of \f5ksh\fP makes it easy to make commands
   3213 built-ins without changing the semantics at all.
   3214 Systems that have run-time binding of libraries allow
   3215 applications to be sped up by supplying the critical
   3216 programs as shell built-in commands.
   3217 Implementations on other systems can add built-in commands
   3218 at compile time.
   3219 The procedure for writing built-in commands that can be loaded
   3220 at run time is in a separate document.\*(Rf,
   3221 .RS
   3222 David Korn,
   3223 .IR "Guidelines for writing \f5ksh-93\fP built-in commands,"
   3224 to be published, 1994.
   3225 .RF
   3226 .H 1  "CONCLUSION"
   3227 .P
   3228 The 1988 version of \f5ksh\fP has tens of thousands of regular users
   3229 and is a suitable replacement for the Bourne shell.
   3230 The 1993 version of \f5ksh\fP is essentially upward compatible with
   3231 both the 1988 version of \f5ksh\fP and with the recent IEEE POSIX
   3232 and ISO shell standard.
   3233 The 1993 version offers many advantages for programming applications,
   3234 and it has been rewritten so that it can be used in embedded applications.
   3235 It also offers improved performance.
   3236 .SG dgk \"  signature typist initials 
   3237 \" .CS 14 24 38 0 0 16  \" cover sheet for TM
   3238 .bp
   3239 .ce
   3240 \fIAPPENDIX\fP
   3241 .nf
   3242 \f5
   3243 .ta .66i 1.33i 2i 2.66i 3.33i 4i 4.66i 5.33i 6i 6.66i 7.33i 8i
   3244 .so grep.mm
   3245 .fi
   3246 \fP
   3247 
   3248 
   3249