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