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