ksh93.memo 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838
  1. Bell Laboratories
  2. subject: Introduction to ksh-93 date: January 14, 1993
  3. Charge Case 311531-0101
  4. File Case 49059-6 from: David G. Korn
  5. MH 11267
  6. 3C-526B x7975
  7. (ulysses!dgk)
  8. TM 11267-930???-93
  9. ABSTRACT
  10. ksh-93 is a major rewrite of ksh, a program that serves as a
  11. command language (shell) for the UNIX* operating system.
  12. As with ksh, ksh-93 is essentially compatible with the
  13. System V version of the Bourne shell[1] , and compatible
  14. with previous versions of ksh. ksh-93 is intended to comply
  15. with the IEEE POSIX 1003.2 shell standard and the ISO 9945-
  16. 2[2] shell standard. In addition to changes in the language
  17. required by these standards, the primary focus of ksh-93 is
  18. related to shell programming. ksh-93 provides the
  19. programming power of several other interpretive languages
  20. such as awk[3], FIT[4], perl[5], and tcl[6].
  21. This memo assumes that the reader is already familiar with
  22. the Bourne shell. It introduces most of the features of
  23. ksh-93 relative to the Bourne shell; both as a command
  24. language and as a programming language. The Appendix
  25. contains a sample script written in ksh-93.
  26. __________
  27. * UNIX is a registered trademark of USL
  28. BELL LABORATORIES PROPRIETARY
  29. Not for use or disclosure outside Bell Laboratories except by
  30. written approval of the director of the distributing organization.
  31. Bell Laboratories
  32. subject: Introduction to ksh-93 date: January 14, 1993
  33. Charge Case 311531-0101
  34. File Case 49059-6 from: David G. Korn
  35. MH 11267
  36. 3C-526B x7975
  37. (ulysses!dgk)
  38. TM 11267-930???-93
  39. MEMORANDUM_FOR_FILE
  40. 1. INTRODUCTION
  41. The term "shell" is used to describe a program that provides
  42. a command language interface. Because the UNIX* system
  43. shell is a user level program, and not part of the operating
  44. system itself, anyone can write a new shell or modify an
  45. existing one. This has caused an evolutionary progress in
  46. the design and implementation of shells, with the better
  47. ones surviving. The most widely available UNIX system
  48. shells are the Bourne shell[7], written by Steve Bourne at
  49. AT&T Bell Laboratories, the C shell[8], written by Bill Joy
  50. at the University of California, Berkeley, and the KornShell
  51. language [9], written by David Korn at AT&T Bell
  52. Laboratories. The Bourne shell is available on almost all
  53. versions of the UNIX system. The C Shell is available with
  54. all Berkeley Software Distribution, BSD, UNIX systems and on
  55. many other systems. The KornShell, is available on System V
  56. Release 4 systems. In addition, it is available on many
  57. other systems. The source for the KornShell language is
  58. available from the AT&T Toolchest, an electronic software
  59. distribution system. It runs on all known versions of the
  60. UNIX system and on many UNIX system look-alikes.
  61. There have been several articles comparing the UNIX system
  62. shells. Jason Levitt[10] highlights some of the new
  63. __________
  64. * UNIX is a registered trademark of USL
  65. BELL LABORATORIES PROPRIETARY
  66. Not for use or disclosure outside Bell Laboratories except by
  67. written approval of the director of the distributing organization.
  68. - 2 -
  69. features introduced by the KornShell language. Rich
  70. Bilancia[11] explains some of the advantages of using the
  71. KornShell language. John Sebes[12] provides a more detailed
  72. comparison of the three shells, both as a command language
  73. and as a programming language.
  74. The KornShell language is a superset of the Bourne shell.
  75. The KornShell language has many of the popular C shell
  76. features, plus additional features of its own. Its initial
  77. popularity stems primarily from its improvements as a
  78. command language. The primary interactive benefit of the
  79. KornShell command language is a visual command line editor
  80. that allows you to make corrections to your current command
  81. line or to earlier command lines, without having to retype
  82. them.
  83. However, in the long run, the power of the KornShell
  84. language as a high-level programming language, as described
  85. by Dolotta and Mashey[13], may prove to be of greater
  86. significance. ksh-93 provides the programming power of
  87. several other interpretive languages such as awk, FIT, perl,
  88. and tcl. An application that was originally written in the
  89. C programming language was rewritten in the KornShell
  90. language. More than 20,000 lines of C code were replaced
  91. with KornShell scripts totaling fewer than 700 lines. In
  92. most instances there was no perceptible difference in
  93. performance between the two versions of the code.
  94. The KornShell language has been embedded into windowing
  95. systems allowing graphical user interfaces to be developed
  96. in shell rather than having to build applications that need
  97. to be compiled. The wksh program[14], provides a method of
  98. developing OpenLook or Motif applications as ksh scripts.
  99. This memo is an introduction to ksh-93 the program that
  100. implements an enhanced version of the KornShell language.
  101. It is referred to as ksh in the rest of this memo. The memo
  102. describes the KornShell language based on the features of
  103. the 02/25/93 release of ksh. This memo is not a tutorial,
  104. only an introduction. The second edition of reference [9]
  105. gives a more complete treatment of the KornShell language.
  106. A concerted effort has been made to achieve both System V
  107. Bourne shell compatibility and IEEE POSIX compatibility so
  108. that scripts written for either of these shells can run
  109. without modification with ksh. In addition, ksh-93 attempts
  110. BELL LABORATORIES PROPRIETARY
  111. Not for use or disclosure outside Bell Laboratories except by
  112. written approval of the director of the distributing organization.
  113. - 3 -
  114. to be compatible with older versions of ksh. When conflicts
  115. between these versions of the shell, ksh-93 selects the
  116. behavior dictated by the IEEE POSIX standard. The
  117. description of features in this memo assumes that the reader
  118. is already familiar with the Bourne shell.
  119. 2. COMMAND LANGUAGE
  120. There is no separate command language. All features of the
  121. language, except job control, can be used both within a
  122. script and interactively from a terminal. However, features
  123. that are more likely to be used while running commands
  124. interactively from a terminal are presented here.
  125. 2.1 Setting Options
  126. By convention, UNIX commands consist of a command name
  127. followed by options and other arguments. Options are either
  128. of the form -letter, or -letter value. In the former case,
  129. several options may be grouped after a single -. The
  130. argument -- signifies an end to the option list and is only
  131. required when the first non-option argument begins with a -.
  132. Most commands print an error message which shows which
  133. options are permitted when given incorrect arguments.
  134. Ordinarily, ksh executes a command by using the command name
  135. to locate a program to run and by running the program as a
  136. separate process. Some commands, referred to as built-ins,
  137. are carried out by ksh itself, without creating a separate
  138. process. The reasons that some commands are built-in are
  139. presented later. In nearly all cases the distinction
  140. between a command that is built-in and one that is not is
  141. invisible to the user. However, nearly all commands that
  142. are built-in follow command line conventions. In addition,
  143. the option sequence -? causes the command to print a usage
  144. message which lists the valid options.
  145. ksh has several options that can be set by the user as
  146. command line arguments and as option arguments to the set
  147. command. Most options can be set with a single letter
  148. option or as a name that follows the -o option. Use set -o
  149. to display the current option settings. Some of these
  150. options, such as interactive and monitor (See Job Control
  151. below) are enabled automatically by ksh when the shell is
  152. connected to a terminal device. Other options, such as
  153. noclobber and ignoreeof are normally placed in a startup
  154. BELL LABORATORIES PROPRIETARY
  155. Not for use or disclosure outside Bell Laboratories except by
  156. written approval of the director of the distributing organization.
  157. - 4 -
  158. file. The noclobber option causes ksh to print an error
  159. message when you use > to redirect to a file that already
  160. exists, If you want to redirect to an existing file, then
  161. you have to use >| to override the noclobber option. The
  162. ignoreeof option is used to prevent the end-of-file
  163. character, normally ^D(Control-d), from exiting the shell
  164. and possibly logging you out. You must type exit to log
  165. out. Most of the options are described in this memo as
  166. appropriate.
  167. 2.2 Command Aliases
  168. Command aliases provide a mechanism of associating a command
  169. name and options with a shorter name. Aliases are defined
  170. with the alias built-in. The form of an alias command
  171. definition is:
  172. alias name=value
  173. As with other shell assignments, no space is allowed before
  174. or after the =. The characters of an alias name cannot be
  175. characters that are special to the shell. The replacement
  176. string, value, can contain any valid shell script, including
  177. meta-characters such as pipe symbols and i/o-redirection
  178. provided that they are quoted. Unlike csh, aliases in ksh
  179. cannot take arguments. The equivalent functionality of
  180. aliases with arguments can only be achieved with shell
  181. fucntions described later.
  182. As a command is being read, the command name is checked
  183. against a list of alias names. If it is found, the name is
  184. replaced by the alias value associated with the alias and
  185. then rescanned. When rescanning the value for an alias,
  186. alias substitutions are performed except for an alias that
  187. is currently being processed. This prevents infinite loops
  188. in alias substitutions. For example with the aliases,
  189. alias l=ls 'ls=ls -C', the command name l becomes ls, which
  190. becomes ls -C. Ordinarily, only the command name word is
  191. processed for alias substitution. However, if the value of
  192. an alias ends in a space, then the word following the alias
  193. is also checked for alias substitution. This makes it
  194. possible to define an alias whose first argument is the name
  195. of a command and have alias substitution performed on this
  196. argument, for example nohup='nohup '.
  197. Aliases can be used to redefine built-in commands so that
  198. the alias,
  199. alias test=./test
  200. BELL LABORATORIES PROPRIETARY
  201. Not for use or disclosure outside Bell Laboratories except by
  202. written approval of the director of the distributing organization.
  203. - 5 -
  204. can be used to look for test in your current working
  205. directory rather than using the built-in test command.
  206. Reserved words such as for and while cannot be changed by
  207. aliasing. The command alias, without arguments, generates a
  208. list of aliases and corresponding alias values. The unalias
  209. command removes the name and text of an alias.
  210. Aliases are used to save typing and to improve readability
  211. of scripts. Several aliases are predefined by ksh. For
  212. example, the predefined alias
  213. alias integer='typeset -i'
  214. allows the integer variables i and j to be declared and
  215. initialized with the command
  216. integer i=0 j=1
  217. While aliases can be defined in scripts, it is not
  218. recommended. The location of an alias command can be
  219. important since aliases are only processed when a command is
  220. read. A . procedure (the shell equivalent to an include
  221. file) is read all at once (unlike start up files which are
  222. read a command at a time) so that any aliases defined there
  223. will not effect any commands within this script. Predefined
  224. aliases do not have this problem.
  225. 2.3 Command Re-entry
  226. When run interactively, ksh saves the commands you type at a
  227. terminal in a file. If the variable HISTFILE is set to the
  228. name of a file to which the user has write access, then the
  229. commands are stored in this history file. Otherwise the
  230. file $HOME/.sh_history is checked for write access and if
  231. this fails an unnamed file is used to hold the history
  232. lines. Commands are always appended to this file.
  233. Instances of ksh that run concurrently and use the same
  234. history file name, share access to the history file so that
  235. a command entered in one shell will be available for editing
  236. in another shell. The file may be truncated when ksh
  237. determines that no other shell is using the history file.
  238. The number of commands accessible to the user is determined
  239. by the value of the HISTSIZE variable at the time the shell
  240. is invoked. The default value is 128. Each command may
  241. consist of one or more lines since a compound command is
  242. considered one command. If the character ! is placed
  243. within the primary prompt string, PS1, then it is replaced
  244. by the command number each time the prompt is given.
  245. BELL LABORATORIES PROPRIETARY
  246. Not for use or disclosure outside Bell Laboratories except by
  247. written approval of the director of the distributing organization.
  248. - 6 -
  249. A built-in command named hist is used to list and/or edit
  250. any of these saved commands. The option -l is used to
  251. specify listing of previous commands. The command can
  252. always be specified with a range of one or more commands.
  253. The range can be specified by giving the command number,
  254. relative or absolute, or by giving the first character or
  255. characters of the command. When given without specifying
  256. the range, the last 16 commands are listed, each preceded by
  257. the command number.
  258. If the listing option is not selected, then the range of
  259. commands specified, or the last command if no range is
  260. given, is passed to an editor program before being re-
  261. executed by ksh. The editor to be used may be specified
  262. with the option -e and following it with the editor name.
  263. If this option is not specified, the value of the shell
  264. variable HISTEDIT is used as the name of the editor,
  265. providing that this variable has non-null value. If this
  266. variable is not set, or is null, and the -e option has not
  267. been selected, then /bin/ed is used. When editing has been
  268. complete, the edited text automatically becomes the input
  269. for ksh. As this text is read by ksh, it is echoed onto the
  270. terminal.
  271. The -s option causes the editing to be bypassed and just
  272. re-executes the command. In this case only a single command
  273. can be specified as the range and an optional argument of
  274. the form old=new may be added which requests a simple string
  275. substitution prior to evaluation. A convenient alias,
  276. alias r='hist -s'
  277. has been pre-defined so that the single key-stroke r can be
  278. used to re-execute the previous command and the key-stroke
  279. sequence, r abc=def c can be used to re-execute the last
  280. command that starts with the letter c with the first
  281. occurrence of the string abc replaced with the string def.
  282. Typing r c > file re-executes the most recent command
  283. starting with the letter c, with standard output redirected
  284. to file.
  285. 2.4 In-line editing
  286. Lines typed from a terminal frequently need changes made
  287. before entering them. With the Bourne shell the only method
  288. to fix up commands is by backspacing or killing the whole
  289. line. ksh offers options that allow the user to edit parts
  290. of the current command line before submitting the command.
  291. BELL LABORATORIES PROPRIETARY
  292. Not for use or disclosure outside Bell Laboratories except by
  293. written approval of the director of the distributing organization.
  294. - 7 -
  295. The in-line edit options make the command line into a single
  296. line screen edit window. When the command is longer than
  297. the width of the terminal, only a portion of the command is
  298. visible. Moving within the line automatically makes that
  299. portion visible. Editing can be performed on this window
  300. until the return key is pressed. The editing modes have
  301. editing directives that access the history file in which
  302. previous commands are saved. A user can copy any of the
  303. most recent HISTSIZE commands from this file into the input
  304. edit window. You can locate commands by searching or by
  305. position.
  306. The in-line editing options do not use the termcap or
  307. terminfo databases. They work on most standard terminals.
  308. They only require that the backspace character moves the
  309. cursor left and the space character overwrites the current
  310. character on the screen and moves the cursor to the right.
  311. Very few terminals or terminal emulators do not have this
  312. behavior.
  313. There is a choice of editor options. The emacs, gmacs, or
  314. vi option is selected by turning on the corresponding option
  315. of the set command. If the value of the EDITOR or VISUAL
  316. variables ends with any of these suffixes the corresponding
  317. option is turned on. A large subset of each of these
  318. editors' features are available within the shell.
  319. Additional functions, such as file name completion, have
  320. also been added.
  321. In the emacs or gmacs mode the user positions the cursor to
  322. the point needing correction and inserts, deletes, or
  323. replaces characters as needed. The only difference between
  324. these two modes is the meaning of the directive ^T. Control
  325. keys and escape sequences are used for cursor positioning
  326. and control functions. The available editing functions are
  327. listed in the manual page.
  328. The vi editing mode starts in insert mode and enters control
  329. mode when the user types ESC ( 033 ). The return key, which
  330. submits the current command for processing, can be entered
  331. from either mode. The cursor can be anywhere on the line.
  332. A subset of commonly used vi editing directives are
  333. available. The k and j directives that normally move up and
  334. down by one line, move up and down one command in the
  335. history file, copying the command into the input edit
  336. window. For reasons of efficiency, the terminal is kept in
  337. BELL LABORATORIES PROPRIETARY
  338. Not for use or disclosure outside Bell Laboratories except by
  339. written approval of the director of the distributing organization.
  340. - 8 -
  341. canonical mode until an ESC is typed. On some terminals,
  342. and on earlier versions of the UNIX operating system, this
  343. doesn't work correctly. The viraw option, which always uses
  344. raw or cbreak mode, must be used in this case.
  345. Most of the code for the editing options does not rely on
  346. the ksh code and can be used in a stand-alone mode with most
  347. any command to add in-line edit capability. However, all
  348. versions of the in-line editors have some features that use
  349. some shell specific code. For example, with all edit modes,
  350. the ESC-= directive applied to command words (the first word
  351. on the line, or the first word after a ;, |, (, or &) lists
  352. all aliases, functions, or commands that match the portion
  353. of the given current word. When applied to other words,
  354. this directive prints the names of files that match the
  355. current word. The ESC-* directive adds the expanded list of
  356. matching files to the command line. A trailing * is added
  357. to the word if it doesn't contain any file pattern matching
  358. characters before the expansion. In emacs and gmacs mode,
  359. ESC-ESC indicates command completion when applied to command
  360. names, otherwise it indicates pathname completion. With
  361. command or pathname completion, the list generated by the
  362. ESC-= directive is examined to find the longest common
  363. prefix. With command completion, only the last component of
  364. the pathname is used to compute the longest command prefix.
  365. If the longest common prefix is a complete match, then word
  366. is replaced by the pathname, and a / is appended if pathname
  367. is a directory, otherwise a space is added. In vi mode, \
  368. from control mode gives the same behavior.
  369. 2.5 Key Binding
  370. It is possible to intercept keys as they are entered and
  371. apply new meanings or bindings. A trap named KEYBD is
  372. evaluated each time the user enters a key from the keyboard,
  373. except while entering a search string or an argument to an
  374. edit directive such as r in vi-mode. The action associated
  375. with this trap can change the value of the entered key to
  376. cause the key to perform a different operation.
  377. When the KEYBD trap is entered, the .sh.edtext variable
  378. contains the contents of the current input line and the
  379. .sh.edcol variable gives the current cursor position within
  380. this line. The .sh.edmode variable contains the ESC
  381. character when the trap is entered from insert mode of vi
  382. mode. Otherwise, this value is null. The .sh.edchar
  383. BELL LABORATORIES PROPRIETARY
  384. Not for use or disclosure outside Bell Laboratories except by
  385. written approval of the director of the distributing organization.
  386. - 9 -
  387. variable contains the character or escape sequence that
  388. caused the trap. The value of .sh.edchar at the end of the
  389. trap will be used as the input sequence.
  390. Using the associative array facility of ksh described later,
  391. and the function facility of ksh, it is easy to write a
  392. single trap so that keys can be bound dynamically. For
  393. example,
  394. typeset -A Keybind
  395. trap 'eval "${Keybind[${.sh.edchar}]}"' KEYBD
  396. function keybind # key seq
  397. {
  398. Keybind[$1]=".sh.edchar=${.sh.edmode}$2"
  399. }
  400. 2.6 Job Control
  401. The job control mechanism is almost identical to the version
  402. found in csh of the Berkeley UNIX operating system, version
  403. 4.1 and later. The job control feature allows the user to
  404. stop and restart programs, and to move programs to and from
  405. the foreground and the background. It will only work on
  406. systems that provide support for these features. However,
  407. even systems without job control have a monitor option which
  408. when enabled, will report the progress of background jobs
  409. and enable the user to kill jobs by job number or job name.
  410. An interactive shell associates a job with each pipeline
  411. typed in from the terminal and assigns them a small integer
  412. number called the job number. If the job is run
  413. asynchronously, the job number is printed at the terminal.
  414. At any given time, only one job owns the terminal, i.e.,
  415. keyboard signals are only sent to the processes in one job.
  416. When ksh creates a foreground job, it gives it ownership of
  417. the terminal. If you are running a job and wish to stop it
  418. you hit the key ^Z (control-Z) which sends a STOP signal to
  419. all processes in the current job. The shell receives
  420. notification that the processes have stopped and takes back
  421. control of the terminal.
  422. There are commands to continue programs in the foreground
  423. and background. There are several ways to refer to jobs.
  424. The character % introduces a job name. You can refer to
  425. jobs by name or number as described in the manual page. The
  426. BELL LABORATORIES PROPRIETARY
  427. Not for use or disclosure outside Bell Laboratories except by
  428. written approval of the director of the distributing organization.
  429. - 10 -
  430. built-in command bg allows you to continue a job in the
  431. background, while the built-in command fg allows you to
  432. continue a job in the foreground even though you may have
  433. started it in the background.
  434. A job being run in the background will stop if it tries to
  435. read from the terminal. It is also possible to stop
  436. background jobs that try to write on the terminal by setting
  437. the terminal options appropriately.
  438. There is a built-in command jobs that lists the status of
  439. all running and stopped jobs. In addition, you are informed
  440. of the change of state (running or stopped) of any
  441. background jobs just before each prompt. If you want to be
  442. notified about background job completions as soon as they
  443. occur without waiting for a prompt, then use the notify
  444. option. When you try to exit the shell while jobs are
  445. stopped or running, you will receive a message from ksh. If
  446. you ignore this message and try to exit again, all stopped
  447. processes will be terminated. In addition, for login
  448. shells, the HUP signal will be sent to all background jobs
  449. unless the job has been disowned with the disown command.
  450. A built-in version of kill makes it possible to use job
  451. numbers as targets for signals. Signals can be selected by
  452. number or name. The name of the signal is the name found in
  453. the include file /usr/include/sys/signal.h with the prefix
  454. SIG removed. The -l option of kill provides a means to map
  455. individual signal names to and from signal number. In
  456. addition, if no signal name or number is given, kill -l
  457. generates a list of valid signal names.
  458. 2.7 Changing Directories
  459. By default, ksh maintains a logical view of the file system
  460. hierarchy which makes symbolic links transparent. For
  461. systems that have symbolic links, this means that if /bin is
  462. a symbolic link to /usr/bin and you change directory to
  463. /bin, pwd will indicate that you are in /bin, not /usr/bin.
  464. pwd -P generates the physical pathname of the present
  465. working directory by resolving all the symbolic links. By
  466. default, the cd command will take you where you expect to go
  467. even if you cross symbolic links. A subsequent cd .. in the
  468. example above will place you in /, not /usr. On systems
  469. with symbolic links, cd -P causes .. to be treated
  470. physically.
  471. BELL LABORATORIES PROPRIETARY
  472. Not for use or disclosure outside Bell Laboratories except by
  473. written approval of the director of the distributing organization.
  474. - 11 -
  475. ksh remembers your last directory in the variable OLDPWD.
  476. The cd built-in can be given with argument - to return to
  477. the previous directory and prints the name of the directory.
  478. Note that cd - done twice returns you to the starting
  479. directory, not the second previous directory. A directory
  480. stack manager has been written as shell functions to push
  481. and pop directories from the stack.
  482. 2.8 Prompts
  483. When ksh reads commands from a terminal, it issues a prompt
  484. whenever it is ready to accept more input and then waits for
  485. the user to respond. The TMOUT variable can be set to be
  486. the number of seconds that the shell will wait for input
  487. before terminating. A 60 second warning message is printed
  488. before terminating.
  489. The shell uses two prompts. The primary prompt, defined by
  490. the value of the PS1 variable, is issued at the start of
  491. each command. The secondary prompt defined by the value of
  492. the PS2 variable, is issued when more input is needed to
  493. complete a command.
  494. ksh allows that user to specify a list of files or
  495. directories to check before issuing the PS1 prompt. The
  496. variable MAILPATH is a colon ( : ) separated list of file
  497. names to be checked for changes periodically. The user is
  498. notified before the next prompt. Each of the names in this
  499. list can be followed by a ? and a prompt to be given when a
  500. change has been detected in the file. The prompt will be
  501. evaluated for parameter substitution. The parameter $_
  502. within a mail message will evaluate to the name of the file
  503. that has changed. The parameter MAILCHECK is used to
  504. specify the minimal interval in seconds before new mail is
  505. checked for.
  506. In addition to replacing each ! in the prompt with the
  507. command version, ksh expands the value of the PS1 variable
  508. for parameters expansions, arithmetic expansions, and
  509. command substitutions as described below to generate the
  510. prompt. The expansion characters that are to be applied
  511. when the prompt is issued must be quoted to prevent the
  512. expansions from occurring when assigning the value to PS1.
  513. For example, PS1="$PWD" causes PS1 to be set to the value of
  514. PWD at the time of the assignment whereas PS1='$PWD' causes
  515. PWD to be expanded at the time the prompt is issued.
  516. BELL LABORATORIES PROPRIETARY
  517. Not for use or disclosure outside Bell Laboratories except by
  518. written approval of the director of the distributing organization.
  519. - 12 -
  520. Command substitution may require a separate process to
  521. execute and cause the prompt display to be somewhat slow,
  522. especially when the return key is pressed several times in a
  523. row. Therefore, its use within PS1 is discouraged. Some
  524. variables are maintained by ksh so that their values can be
  525. used with PS1. The PWD variable stores the pathname of the
  526. current working directory. The value of SECONDS variable is
  527. the value of the most recent assignment plus the elapsed
  528. time. By default, the time is measured in milli-seconds,
  529. but since SECONDS is a floating point variable, the number
  530. of places after the decimal point in the expanded value can
  531. be specified with typeset -Fplaces SECONDS. In a roundabout
  532. way, this variable can be used to generate a time stamp into
  533. the PS1 prompt without creating a process at each prompt.
  534. The following code explains how you can do this on System V.
  535. On BSD, you need another command to initialize the SECONDS
  536. variable.
  537. # . this script and use $TIME as part of your PS1 string to
  538. # get the time of day in your prompt
  539. typeset -RZ2 _x1 _x2 _x3
  540. (( SECONDS=$(date '+3600*%H+60*%M+%S') ))
  541. _s='_x1=(SECONDS/3600)%24,_x2=(SECONDS/60)%60,_x3=SECONDS%60,0'
  542. TIME='"${_d[_s]}$_x1:$_x2:$_x3"'
  543. # PS1=${TIME}whatever
  544. 2.9 Tilde substitution
  545. The character ~ at the beginning of a word has special
  546. meaning to ksh. If the characters after the ~ up to a /
  547. match a user login name in the password database, then the ~
  548. and the name are replaced by that user's login directory.
  549. If no match is found, the original word is unchanged. A ~
  550. by itself, or in front of a /, is replaced by the value of
  551. the HOME parameter. A ~ followed by a + or - is replaced by
  552. the value of $PWD and $OLDPWD respectively.
  553. 2.10 Output formats
  554. The output of built-in commands and traces have values
  555. quoted so that they can be re-input to the shell. This
  556. makes it easy to use cut and paste shell output on systems
  557. which use a pointing device such as a mouse. In addition,
  558. BELL LABORATORIES PROPRIETARY
  559. Not for use or disclosure outside Bell Laboratories except by
  560. written approval of the director of the distributing organization.
  561. - 13 -
  562. output can be saved in a file for reuse.
  563. 2.11 The ENV file
  564. When an interactive ksh starts, it evaluates the $ENV
  565. variable to arrive at a file name. If this value is not
  566. null, ksh attempts to read and process commands in a file by
  567. this name. Earlier versions of ksh read the ENV file for
  568. all invocations of the shell primarily to allow function
  569. definitions to be available for all shell invocations. The
  570. function search path, FPATH, described later, eliminated the
  571. primary need for this capability and it was removed because
  572. the high performance cost was no longer deemed acceptable.
  573. 3. PROGRAMMING LANGUAGE
  574. The KornShell vastly extends the set of applications that
  575. can be implemented efficiently at the shell level. It does
  576. this by providing simple yet powerful mechanisms to perform
  577. arithmetic, pattern matching, substring generation, and
  578. arrays. Users can write applications as separate functions
  579. that can be defined in the same file or in a library of
  580. functions stored in a directory and loaded on demand.
  581. 3.1 String Processing
  582. The shell is primarily a string processing language. By
  583. default, variables hold variable length strings. There are
  584. no limits to the length of strings. Storage management is
  585. handled by the shell automatically. Declarations are not
  586. required. With most programming languages, string constants
  587. are designated by enclosing characters in single quotes or
  588. double quotes. Since most of the words in the language are
  589. strings, the shell requires quotes only when a string
  590. contains characters that are normally processed specially by
  591. the shell, but their literal meaning is intended. However,
  592. since the shell is a string processing language, and some
  593. characters can occur as literals and as language
  594. metacharacters, quoting is an important part of the
  595. language.
  596. There are four quoting mechanisms in ksh. The simplest is
  597. to enclose a sequence of characters inside single quotes.
  598. All characters between a pair of single quotes have their
  599. literal meaning; the single quote itself cannot appear. A $
  600. BELL LABORATORIES PROPRIETARY
  601. Not for use or disclosure outside Bell Laboratories except by
  602. written approval of the director of the distributing organization.
  603. - 14 -
  604. immediately preceding a single quoted string causes all the
  605. characters until the matching single quote to be interpreted
  606. as an ANSI-C language string. Thus, '\n' represents
  607. characters \ and n, whereas, $'\n' represents the new-line
  608. character. Double quoted strings remove the special meaning
  609. of all characters except $, and `, so that parameter
  610. expansion and command substitution (defined below) are
  611. performed. The final mechanism for quoting a character is
  612. by preceding it with the escape character \. This mechanism
  613. works outside of quoted strings and for the characters $, `,
  614. ", and \ in double quoted strings.
  615. Variables are designated by one or more strings of
  616. alphanumeric characters beginning with an alphabetic
  617. character separated by a .. Upper and lowercase characters
  618. are distinct, so that the variable A and a are names of
  619. different variables. There is no limit to the length of the
  620. name of a variable. You do not have to declare variables.
  621. You can assign a value to a variable by writing the name of
  622. the variable, followed by an equal sign, followed by a
  623. character string that represents its value. To create a
  624. variable whose name contains a ., the variable whose name
  625. consists of the characters before the last . must already
  626. exist. You reference a variable by putting the name inside
  627. curly braces and preceding the braces with a dollar sign.
  628. The braces may be omitted when the name is alphanumeric. If
  629. x and y are two shell variables, then to define a new
  630. variable, z, whose value is the concatenation of the values
  631. of x and y, you just say z=$x$y. It is that easy.
  632. The $ can be thought of as meaning "value of." You can also
  633. capture the output of any command with the notation
  634. $(command). This is referred to as command substitution.
  635. For example, x=$(date) assigns the output from the date
  636. command to the variable x. Command substitution in the
  637. Bourne shell is denoted by enclosing the command between
  638. backquotes, (``). This notation suffers from some
  639. complicated quoting rules. Thus, it is hard to write sed
  640. patterns which contains back slashes within command
  641. substitution. Putting the pattern in single quotes is of
  642. little help. ksh accepts the Bourne shell command
  643. substitution syntax for backward compatibility. The
  644. $(command) notation allows the command itself to contain
  645. quoted strings even if the substitution occurs within double
  646. quotes. Nesting is legal.
  647. BELL LABORATORIES PROPRIETARY
  648. Not for use or disclosure outside Bell Laboratories except by
  649. written approval of the director of the distributing organization.
  650. - 15 -
  651. The special command substitution of the form $(cat file) can
  652. be replaced by $(< file), which is faster because the cat
  653. command doesn't have to run.
  654. 3.2 Shell Parameters and Variables
  655. There are three types of parameters used by ksh, special
  656. parameters, positional parameters, and named parameters
  657. which are called variables. ksh defines the same special
  658. characters, 0, *, @, #, ?, $, !, and -, as they are defined
  659. in the Bourne shell.
  660. Positional parameters are set when the shell is invoked, as
  661. arguments to the set built-in, and by calls to functions
  662. (see below) and . procedures. They are named by a number
  663. starting at 1.
  664. The third type of parameter is a variable. As mentioned
  665. earlier, ksh uses variables whose names consist of one or
  666. more alpha-numeric strings separated by a .. There is no
  667. need to specify the type of a variable in the shell because,
  668. by default, variables store strings of arbitrary length and
  669. values will automatically be converted to numbers when used
  670. in an arithmetic context. However, ksh variables can have
  671. one or more attributes that control the internal
  672. representation of the variable, the way the variable is
  673. printed, and its access or scope. In addition, ksh allows
  674. variables to represent arrays of values and references to
  675. other variables. The typeset built-in command of ksh
  676. assigns attributes to variables. Two of the attributes,
  677. readonly and export, are available in the Bourne shell.
  678. Most of the remaining attributes are discussed here. The
  679. complete list of attributes appears in the manual. The
  680. unset built-in of ksh removes values and attributes of
  681. variables. When a variable is exported, certain of its
  682. attributes are also exported.
  683. Whenever a value is assigned to a variable, the value is
  684. transformed according to the attributes of the variable.
  685. Changing the attribute of a variable can change its value.
  686. The attributes -L and -R are for left and right field
  687. justification respectively. They are useful for aligning
  688. columns in a report. For each of these attributes, a width
  689. can be defined explicitly or else it is defined the first
  690. time an assignment is made to the variable. Each assignment
  691. causes justification of the field, truncating if necessary.
  692. BELL LABORATORIES PROPRIETARY
  693. Not for use or disclosure outside Bell Laboratories except by
  694. written approval of the director of the distributing organization.
  695. - 16 -
  696. Assignment to fixed sized variables provides one way to
  697. generate a substring consisting of a fixed number of
  698. characters from the beginning or end of a string. Other
  699. methods are discussed later.
  700. The attributes -u and -l, are used for upper case and lower
  701. case formatting respectively. Since it makes no sense to
  702. have both attributes on simultaneously, turning on either of
  703. these attributes turns the other off. The following script,
  704. using read and print which are described later, provides an
  705. example of the use of shell variables with attributes. This
  706. script reads a file of lines each consisting of five fields
  707. separated by : and prints fields 4 and 2 in upper case in
  708. columns 1-15, left justified, and columns 20-25 right-
  709. justified respectively.
  710. typeset -uL15 f4 # 15 character left justified
  711. typeset -uR6 f2 # 6 character right justified
  712. IFS=: # set field separator to :
  713. while read -r f1 f2 f3 f4 f5 # read line, split into fields
  714. do print -r -- "$f4 $f2" # print fields 4 and 2
  715. done
  716. The -i, -E, and -F, attributes are used to represent
  717. numbers. Each can be followed by a decimal number. The -i
  718. attribute causes the value to be represented as an integer
  719. and it can be followed by a number representing the numeric
  720. base when expanding its value. Whenever a value is assigned
  721. to an integer variable, it is evaluated as an arithmetic
  722. expression and then truncated to an integer.
  723. The -E attribute causes the value to be represented in
  724. scientific notation whenever its value is expanded. The
  725. number following the -E determines the number of significant
  726. figures, and defaults to 6. The -F attribute causes the
  727. value to be represented with a fixed number of places after
  728. the decimal point. Assignments to variables of the -E or -F
  729. cause the evaluation of the right hand side of the
  730. assignment.
  731. ksh allows one-dimensional arrays in addition to simple
  732. variables. There are two types of arrays; associative
  733. arrays and indexed arrays. The subscript for an associative
  734. array is an arbitrary string, whereas the subscript for an
  735. indexed array is an arithmetic expression that is evaluated
  736. BELL LABORATORIES PROPRIETARY
  737. Not for use or disclosure outside Bell Laboratories except by
  738. written approval of the director of the distributing organization.
  739. - 17 -
  740. to yield an integer index. Any variable can become an
  741. indexed array by referring to it with a subscript. All
  742. elements of an array need not exist. Subscripts for arrays
  743. must evaluate to an integer between 0 and some maximum
  744. value, otherwise an error results. The maximum value may
  745. vary from one machine to another but is at least 4095.
  746. Evaluation of subscripts is described in the next section.
  747. Attributes apply to the whole array.
  748. Assignments to array variables can be made to individual
  749. elements via parameter assignment commands or the typeset
  750. built-in. Additionally, values can be assigned sequentially
  751. using the -A name option of the set command. Referencing of
  752. subscripted variables requires the character $, but also
  753. requires braces around the array element name. The braces
  754. are needed to avoid conflicts with the file name generation
  755. mechanism. The form of any array element reference is:
  756. ${name[subscript]}.
  757. A subscript value of * or @ can be used to generate all
  758. elements of an array, as they are used for expansion of
  759. positional parameters. The list of currently defined
  760. subscripts for a given variable can be generated with
  761. ${!name[@]}, or ${!name[*]}.
  762. The nameref attribute causes the variable to be treated as a
  763. reference to the variable defined by its value. Once this
  764. attribute is set, all references to this variable become
  765. references to the variable named by the value of this
  766. variable. For example, if foo=bar, then setting the
  767. reference attribute on foo will call all subsequent
  768. references to foo to behave as references to bar. Unsetting
  769. this attribute breaks the association. Reference variables
  770. are usually used inside functions whose arguments are the
  771. name of a shell variable. The names for reference variables
  772. cannot contain a .. Whenever a shell variable is
  773. referenced, the portion of the variable up to the first .
  774. is checked to see whether it matches the name of a reference
  775. variable. If it does, then the name of the variable
  776. actually used consists of the concatenation of the name of
  777. the variable defined by the reference plus the remaining
  778. portion of the original variable name. For example, using
  779. the predefined alias, alias nameref='typeset -n',
  780. .bar.home.bam="hello world"
  781. nameref foo=.bar.home
  782. print ${foo.bam}
  783. BELL LABORATORIES PROPRIETARY
  784. Not for use or disclosure outside Bell Laboratories except by
  785. written approval of the director of the distributing organization.
  786. - 18 -
  787. hello world
  788. 3.3 Substring Generation
  789. The expansion of a variable or parameter can be modified so
  790. that only a portion of the value results. It is often
  791. necessary to extract a portion of a shell variable or a
  792. portion of an array. There are several parameter expansion
  793. operators that can do this. One method to generate a
  794. substring is with an expansion of the form
  795. ${name:offset:length} where offset is an arithmetic
  796. expression that defines the offset of the first character
  797. starting from 0, and length is an arithmetic expression that
  798. defines the length of the substring. If :length is omitted,
  799. the length of the value of name starting at offset is used.
  800. The :offset:length operators can also be applied to array
  801. expansions and to parameters * and @ to generate portions of
  802. an array. For example, the expansion,
  803. ${name[@]:offset:length}, yields up to length elements of
  804. the array name starting at the element offset.
  805. The other parameter expansion modifiers use shell patterns
  806. to describe portions of the string to modify and delete. A
  807. description of shell patterns is contained below. When
  808. these modifiers are applied to special parameters @ and * or
  809. to array parameters given as name[@] or name[*], the
  810. operation is performed on each element. There are four
  811. parameter substitution modifiers that strip off leading and
  812. trailing substrings during parameter substitution by
  813. removing the characters matching a given pattern. An
  814. expansion of the form ${name#pattern} causes the smallest
  815. matching prefix of the value of name to be removed. The
  816. largest prefix matching pattern is removed by using ##
  817. instead of #. Similarly, an expansion of the form
  818. ${name%pattern} causes the smallest matching substring at
  819. the end of name to be removed. Again, using %% instead of
  820. %, causes the largest matching trailing substring to be
  821. deleted. For example, if the shell variable file has value
  822. foo.c, then the expression ${file%.c}.o has value foo.o.
  823. The value of an expansion can be changed by specifying a
  824. pattern that matches the part that needs to be changed after
  825. the the parameter expansion modifier /. An expansion of the
  826. form ${name/pattern/string} replaces the first match of
  827. pattern with the value of variable name to string. The
  828. BELL LABORATORIES PROPRIETARY
  829. Not for use or disclosure outside Bell Laboratories except by
  830. written approval of the director of the distributing organization.
  831. - 19 -
  832. second / is not necessary when string is null. The
  833. expansion ${name//pattern/string} changes all occurrences of
  834. the pattern into string. The parameter expansion modifiers
  835. /# and /% cause the matching pattern to be anchored to the
  836. beginning and end respectively.
  837. Finally, there are parameter expansion modifiers that yield,
  838. the name of the variable, the string length of the value or
  839. the number of elements of an array. ${!name} yields the
  840. name of the variable which will be name itself except when
  841. name is a reference variable. In this case it will yield
  842. the name of the variable it refers to. ${#name} will be the
  843. length in bytes of $name. For an array variable ${#name[*]}
  844. gives the number of elements in the array.
  845. 3.4 Arithmetic Evaluation
  846. For the most part, the shell is a string processing
  847. language. However, the need for arithmetic has long been
  848. obvious. Many of the characters that are special to the
  849. Bourne shell, are needed as arithmetic operators. To make
  850. arithmetic easy to use, and to maintain compatibility with
  851. the Bourne shell, ksh uses matching (( and )) to delineate
  852. arithmetic expressions. While single parentheses might have
  853. been more desirable, these already mean subshell so that
  854. another notation was required. The arithmetic expression
  855. inside the double parentheses follows that same syntax,
  856. associativity and precedence as the ANSI-C[15] programming
  857. language. The characters between the matching double
  858. parentheses are processed with the same rules used for
  859. double quotes so that spaces can be used to aid readability
  860. without additional quoting.
  861. All arithmetic evaluations are performed using double
  862. precision floating point arithmetic. Floating point
  863. constants follow the same rules as the ANSI-C programming
  864. language. Integer arithmetic constants are written as
  865. base#number,
  866. where base is a decimal integer between two and sixty-four
  867. and number is any non-negative number. Base ten is used
  868. when no base is specified. The digits are represented by
  869. the characters 0-9a-zA-Z_@. For bases less than or equal to
  870. 36, upper and lower case characters can be used
  871. interchangibly to represent the digits from 10 thru 35.
  872. BELL LABORATORIES PROPRIETARY
  873. Not for use or disclosure outside Bell Laboratories except by
  874. written approval of the director of the distributing organization.
  875. - 20 -
  876. Arithmetic expressions are made from constants, variables,
  877. operators. Parentheses may be used for grouping. The
  878. contents inside the double parentheses are processed with
  879. the same expansions as occurs in a double quoted string, so
  880. that all $ expansions are performed before the expression
  881. are performed. However, there is no need to use the $ to
  882. get the value of a variable because the arithmetic evaluator
  883. replaces the name of the variable by its value within an
  884. arithmetic expression. The $ cannot be used when the
  885. variable is the subject of assignment or an increment
  886. operation. As a rule it is better not to use $ in front of
  887. variables in an arithmetic expression.
  888. An arithmetic command of the form (( ... )) is a command
  889. that evaluates the enclosed arithmetic expression. For
  890. example, the command
  891. (( x++ ))
  892. can be used to increment the variable x, assuming that x
  893. contains some numerical value. The arithmetic command is
  894. true (return value 0), when the resulting expression is
  895. non-zero, and false (return value 1) when the expression
  896. evaluates to zero. This makes the command easy to use with
  897. the if and while compound command.
  898. The for compound command has been extended for use in
  899. arithmetic contexts. The syntax,
  900. for (( expr1; expr2 ; expr3 ))
  901. can be used as the first line of a for loop with the same
  902. semantics as the for statement in the ANSI-C programming
  903. language.
  904. Arithmetic evaluations can also be performed as part of the
  905. evaluation of a command line. The syntax $(( ... )) expands
  906. to the value of the enclosed arithmetic expression. This
  907. expansion can occur wherever parameter expansion is
  908. performed. For example using the ksh command print
  909. (described later)
  910. print $((2+2))
  911. prints the number 4.
  912. The following script prints the first n lines of its
  913. standard input onto its standard output, where n can be
  914. supplied as an optional argument whose default value is 20.
  915. integer n=${1-20} # set n
  916. while (( n-- >=0 )) && read -r line # at most n lines
  917. BELL LABORATORIES PROPRIETARY
  918. Not for use or disclosure outside Bell Laboratories except by
  919. written approval of the director of the distributing organization.
  920. - 21 -
  921. do print -r -- "$line"
  922. done
  923. 3.5 Shell Expansions
  924. The commands you enter from the terminal or from a script
  925. are divided into words and each word undergoes several
  926. expansions to generate the command name and its arguments.
  927. This is done in two phases. The first phase recognizes
  928. reserved words, spaces and operators to decide where command
  929. boundaries lie. Alias substitutions take place during this
  930. phase. The second phase performs expansions in the
  931. following order:
  932. + Tilde substitution, parameter expansion, arithmetic
  933. expansion, and command substitution are performed from
  934. left to right.
  935. + The characters that result from parameter expansion and
  936. command substitution above are checked with the
  937. characters in the IFS variable for possible field
  938. splitting. (See a description of read below to see how
  939. IFS is used.) Setting IFS to a null value causes field
  940. splitting to be skipped.
  941. + Pathname generation (as described below) is performed
  942. on each of the fields. Any field that doesn't match a
  943. pathname is left alone. The option, -f or noglob, is
  944. used to disable pathname generation.
  945. 3.6 Pattern Matching
  946. The shell is primarily a string processing language and uses
  947. patterns for matching file names as well as for matching
  948. strings. The characters ?, *, and [ are processed specially
  949. by the shell when not quoted. These characters are used to
  950. form patterns that match strings. Patterns are used by the
  951. shell to match pathnames, to specify substrings, and for
  952. case commands. The character ? matches any one character.
  953. The character * matches zero or more characters. The
  954. character sequence [...] defines a character class that
  955. matches any character contained within []. A range of
  956. characters can be specified by putting a - between the first
  957. and last character of the range. An exclamation mark, !,
  958. immediately after the [, means match all characters except
  959. BELL LABORATORIES PROPRIETARY
  960. Not for use or disclosure outside Bell Laboratories except by
  961. written approval of the director of the distributing organization.
  962. - 22 -
  963. the characters specified. For example, the pattern
  964. a?c*.[!a-z] matches any string beginning with an a, whose
  965. third character is a c, and that ends in . (dot) followed
  966. by any character except the lowercase letters, a-z. The
  967. sequence [:alpha:] inside a character class, matches any set
  968. of characters in the ANSI-C alpha class. Similarly,
  969. [:class:] matches each of the characters in the given class
  970. for all the ANSI-C character classes.
  971. ksh treats strings of the form (pattern-list) , where
  972. pattern-list is a list of one or more patterns separated by
  973. a |, specially when preceded by *, ?, +, @, or !. A ?
  974. preceding (pattern-list) means that the pattern list
  975. enclosed in () is optional. An @(pattern-list) matches any
  976. pattern in the list of patterns enclosed in (). A
  977. *(pattern-list) matches any string that contains zero or
  978. more of each of the enclosed patterns, whereas +(pattern-
  979. list) requires a match of one or more of any of the given
  980. patterns. For instance, the pattern +([0-9])?(.) matches
  981. one or more digits optionally followed by a .(dot). A
  982. !(pattern-list) matches anything except any of the given
  983. patterns. For example, print !(*.o) will display any file
  984. name that does not end in .o.
  985. When patterns are used to generate pathnames when expanding
  986. commands several other rules apply. A separate match is
  987. made for each matching on each file name component of the
  988. pathname. Read permission is required for any portion of the
  989. pathname that contains any special pattern character. Search
  990. permission is required for every component except possibly
  991. the last.
  992. By default, file names in each directory that begin with .
  993. are skipped when performing a match. If the pattern to be
  994. matched starts with a leading ., then only files beginning
  995. with a ., are considered when finding matching files. If
  996. the FIGNORE variable is set, then only files that do not
  997. match this pattern are considered. This overrides the
  998. special meaning of . in a pattern and in a file name.
  999. If the markdirs option is set, each matching pathname that
  1000. is the name of a directory has a trailing / appended to the
  1001. name.
  1002. BELL LABORATORIES PROPRIETARY
  1003. Not for use or disclosure outside Bell Laboratories except by
  1004. written approval of the director of the distributing organization.
  1005. - 23 -
  1006. 3.7 Conditional Expressions
  1007. The Bourne shell uses the test command, or the equivalent [
  1008. command, to test the attributes for files and to compare
  1009. strings or numbers. The problem with test is that the shell
  1010. has expanded the words of the test command and split them
  1011. into arguments before test begins execution. test cannot
  1012. distinguish between operators and operands. In most cases
  1013. test "$1" will test whether argument 1 is non-null.
  1014. However, if argument 1 is -f, then test will treat -f as an
  1015. operator and yield a syntax error. One of the most frequent
  1016. errors with test occurs when its operands are not within
  1017. double quotes. In this case, the argument may expand to
  1018. more than a single argument or to no argument at all. In
  1019. either case this will likely cause a syntax error. What
  1020. makes this most insidious is that these errors are
  1021. frequently data dependent. A script that appears to run
  1022. correctly may abort if given unexpected data.
  1023. To get around these problems, ksh has a compound command for
  1024. condition expression testing as part of the language. The
  1025. reserved words [[ and ]] delimit the range of the command.
  1026. Because they are reserved words, not operator characters,
  1027. they require spaces to separate them from arguments. The
  1028. words between [[ and ]] are not processed for field
  1029. splitting or for pathname generation. In addition, since
  1030. ksh determines the operators before parameter expansion,
  1031. expansions that yield no argument cause no problem. The
  1032. operators within [[...]] are almost the same as those for
  1033. the test command. All unary operators are of the form
  1034. -letter and are followed by a single operand. Instead of -a
  1035. and -o, [[...]] uses && and || to indicate "and" and "or".
  1036. Parentheses are used without quoting for grouping.
  1037. The right hand side of the string comparison operators ==
  1038. and != take a pattern and tests whether the left hand
  1039. operand matches this pattern. Quoting the pattern results
  1040. is a string comparison rather than the pattern match. The
  1041. operators < and > within [[...]] designate lexicographical
  1042. comparison.
  1043. In addition there are several other new comparison
  1044. primitives. The binary operators -ot and -nt compare the
  1045. modification times of two files to see which file is older
  1046. than or newer than the other. The binary operator -ef tests
  1047. whether two files have the same device and i-node number,
  1048. BELL LABORATORIES PROPRIETARY
  1049. Not for use or disclosure outside Bell Laboratories except by
  1050. written approval of the director of the distributing organization.
  1051. - 24 -
  1052. i. e., a link to the same file.
  1053. The unary operator -L returns true if its operand is a
  1054. symbolic link. The unary operator -O ( -G ) returns true if
  1055. the owner (or group) of the file operand matches that of the
  1056. caller. The unary operator -o returns true when its operand
  1057. is the name of an option that is currently on.
  1058. The following script illustrates some of the uses of
  1059. [[...]]. The reference manual contains the complete list of
  1060. operators.
  1061. for i in "${@}"
  1062. do # execute foo for numeric directory
  1063. if [[ -d $i && $i == +([0-9]) ]]
  1064. then foo
  1065. # otherwise if writable or executable file and not mine
  1066. elif [[ (-w $i||-x $i) && ! -O $i ]]
  1067. then bar
  1068. fi
  1069. done
  1070. 3.8 Input and Output
  1071. ksh has extended I/O capabilities to enhance the use of the
  1072. shell as a programming language. As with the Bourne shell,
  1073. you use the I/O redirection operator, <, to control where
  1074. input comes from, and the I/O redirection operator, >, to
  1075. control where output goes to. Each of these operators can be
  1076. preceded with a single digit that specifies a file unit
  1077. number to associate with the file stream. Ordinarily you
  1078. specify these I/O redirection operators with a specific
  1079. command to which it applies. However, if you specify I/O
  1080. redirections with the exec command, and don't specify
  1081. arguments to exec, then the I/O redirection applies to the
  1082. current program. For example, the command exec < foobar
  1083. opens file foobar for reading. The exec command is also
  1084. used to close files. A file descriptor unit can be opened
  1085. as a copy of an existing file descriptor unit by using
  1086. either of the <& or >& operators and putting the file
  1087. descriptor unit of the original file after the &. Thus,
  1088. 2>&1 means open standard error (file descriptor 2) as a copy
  1089. of standard output (file descriptor 1). A file descriptor
  1090. value of - after the & indicates that the file should be
  1091. closed. To close file unit 5, specify exec 5<&-. There are
  1092. BELL LABORATORIES PROPRIETARY
  1093. Not for use or disclosure outside Bell Laboratories except by
  1094. written approval of the director of the distributing organization.
  1095. - 25 -
  1096. two additional redirection operators with ksh and the POSIX
  1097. shell that are not part of the Bourne shell. The >|
  1098. operator overrides the effect of the noclobber option
  1099. described earlier. The <> operator causes a file to be
  1100. opened for both reading and writing.
  1101. ksh recognizes certain pathnames and treats them specially.
  1102. Pathnames of the form /dev/fd/n are treated as equivalent to
  1103. the file defined by file descriptor n. These name can be
  1104. used as the script argument to ksh and in conditional
  1105. testing as described above. On underlying systems that
  1106. support /dev/fd in the file system, these names can be
  1107. passed to other commands. Pathnames of the form
  1108. /dev/tcp/hostid/port and /dev/udp/hostid/port can be used to
  1109. create tcp and udp connections to services given by the
  1110. hostid number and port number. The hostid cannot use
  1111. symbolic values. In practice these are typically generated
  1112. by command substitution. For example,
  1113. exec 5> /dev/tcp/$(service name) would open file descriptor
  1114. 5 for sending messages to hostid and port number defined by
  1115. the output of service name.
  1116. The Bourne shell has a built-in command read for reading
  1117. lines from standard input (file descriptor 0) and splitting
  1118. it into fields based on the value of the IFS variable, and a
  1119. command echo to write strings to standard output. ( On some
  1120. systems, echo is not a built-in command and incurs
  1121. considerable overhead to use.) Unfortunately, neither of
  1122. these commands is able to perform some very basic tasks.
  1123. For example. with the Bourne shell, the read built-in
  1124. cannot read a single line that end in \. With ksh the read
  1125. built-in has a -r option to remove the special meaning for \
  1126. which allows it to be treated as a regular character rather
  1127. than the line continuation character. With the Bourne
  1128. shell, there is no simple way to have more than one file
  1129. open at any time for reading. ksh has options on the read
  1130. command to specify the file descriptor for the input. The
  1131. fields that are read from a line can be stored into an
  1132. indexed array with the -A option to read. This allows a
  1133. line to be split into an arbitrary number of fields.
  1134. The way the Bourne shell uses the IFS variable to split
  1135. lines into fields greatly limits its utility. Often data
  1136. files consist of lines that use a character such as : to
  1137. delimit fields with two adjacent delimiters that denote a
  1138. null field. The Bourne shell treats adjacent delimiters as
  1139. BELL LABORATORIES PROPRIETARY
  1140. Not for use or disclosure outside Bell Laboratories except by
  1141. written approval of the director of the distributing organization.
  1142. - 26 -
  1143. a single field delimiter. With ksh, delimiters that are
  1144. considered white space characters have the behavior of the
  1145. Bourne shell, but other adjacent delimiters separate null
  1146. fields.
  1147. The read command is often used in scripts that interact with
  1148. the user by prompting the user and then requesting some
  1149. input. With the Bourne shell two commands are needed; one
  1150. to prompt the user, the other to read the reply. ksh allows
  1151. these two commands to be combined. The first argument of
  1152. the read command can be followed by a ? and a prompt string
  1153. which is used whenever the input device is a terminal.
  1154. Because the prompt is associated with the read built-in, the
  1155. built-in command line editors will be able to re-output the
  1156. prompt whenever the line needs to be refreshed when reading
  1157. from a terminal device.
  1158. With the Bourne shell, there is no way to set a time limit
  1159. for waiting for the user response to read. The -t option to
  1160. read takes a floating point argument that gives the time in
  1161. seconds, or fractions of seconds that the shell should wait
  1162. for a reply.
  1163. The version of the echo command in System V treats certain
  1164. sequences beginning with \ as control sequences. This makes
  1165. it hard to output strings without interpretation. Most BSD
  1166. derived systems do not interpret \ control sequences.
  1167. Unfortunately, the BSD versions of echo accepts a -n option
  1168. to prevent a trailing new-line, but has no way to cause the
  1169. string -n to be printed. Neither of these versions is
  1170. adequate. Also, because they are incompatible, it is very
  1171. hard to write portable shell scripts using echo. The ksh
  1172. built-in, print, outputs characters to the terminal or to a
  1173. file and subsumes the functions of all versions of echo.
  1174. Ordinarily, escape sequences in arguments beginning with \
  1175. are processed the same as for the System V echo command.
  1176. However print follows the standard conventions for options
  1177. and has options that make print very versatile. The -r
  1178. option can be used to output the arguments without any
  1179. special meaning. The -n option can be used here to suppress
  1180. the trailing new-line that is ordinarily appended. As with
  1181. read, it is possible to specify the file descriptor number
  1182. as an option to the command to avoid having to use
  1183. redirection operators with each occurrence of the command.
  1184. BELL LABORATORIES PROPRIETARY
  1185. Not for use or disclosure outside Bell Laboratories except by
  1186. written approval of the director of the distributing organization.
  1187. - 27 -
  1188. The IEEE POSIX shell and utilities standard committee was
  1189. unable to reconcile the differences between the System V and
  1190. BSD versions of echo. They introduced a new command named
  1191. printf which takes an ANSI-C format string and a list of
  1192. options and outputs the strings using the ANSI-C formatting
  1193. rules. Since ksh is POSIX conforming, it accepts printf.
  1194. However, there is a -f options to print that can be used to
  1195. specify a format string which processes the arguments the
  1196. same way that printf does.
  1197. The format processing for print and printf has been extended
  1198. slightly. There are three additional formatting directives.
  1199. The %b format causes the \ escape sequences to be expanded
  1200. as they are with the System V echo command. The %q format
  1201. causes quotes to be placed on the output as required so that
  1202. it can be used as shell input. Special characters in the
  1203. output of most ksh built-in commands and in the output from
  1204. an execution trace are quoted in an equivalent fashion. The
  1205. %P format causes an extended regular expression string to be
  1206. converted into a shell pattern. This is useful for writing
  1207. shell applications that have to accept regular expression as
  1208. input. Finally, the escape sequence \E which expands to the
  1209. terminal escape character (octal 033) has been added.
  1210. The shell is frequently used as a programming language for
  1211. interactive dialogues. The select statement has been added
  1212. to the language to make it easier to present menu selection
  1213. alternatives to the user and evaluate the reply. The list
  1214. of alternatives is numbered and put in columns. A user
  1215. settable prompt, PS3, is issued and if the answer is a
  1216. number corresponding to one of the alternatives, the select
  1217. loop variable is set to this value. In any case, the REPLY
  1218. variable is used to store the user entered reply. The shell
  1219. variables LINES and COLUMNS are used to control the layout
  1220. of select lists.
  1221. 3.9 Co-process
  1222. ksh can spawn a co-process by adding a |& after a command.
  1223. This process will be run with its standard input and its
  1224. standard output connected to the shell. The built-in
  1225. command print with the -p option will write into the
  1226. standard input of this process and the built-in command read
  1227. with the -p option will read from the output of this
  1228. process.
  1229. BELL LABORATORIES PROPRIETARY
  1230. Not for use or disclosure outside Bell Laboratories except by
  1231. written approval of the director of the distributing organization.
  1232. - 28 -
  1233. In addition, the I/O redirection operators <& and >& can be
  1234. used to move the input or output pipe of the co-process to a
  1235. numbered file descriptor. Use exec 3>& p to move the input
  1236. of the co-process to file descriptor 3. After you have
  1237. connected to file descriptor 3, you can direct the output of
  1238. any command to the co-process by running command >&3. Also,
  1239. by moving the input of the co-process to a numbered
  1240. descriptor, it is possible to run a second co-process. The
  1241. output of both co-processes will be the file descriptor
  1242. associated with read -p. You can use exec 4< p to cause the
  1243. output of these co-processes to go to file descriptor 4 of
  1244. the shell. Once you have moved the pipe to descriptor 4, it
  1245. is possible to connect a server to the co-process by running
  1246. command 4<& p or to close the co-process pipe with
  1247. exec 4<& -.
  1248. 3.10 Functions
  1249. Function definitions are of the form
  1250. function name
  1251. {
  1252. any shell script
  1253. }
  1254. A function whose name contains a . is called a discipline
  1255. function. The portion of the name before the last . must
  1256. refer to the name of an existing variable. Thus, if p is a
  1257. reference to PATH, then the function name p.get and PATH.get
  1258. refer to the same function.
  1259. The function is invoked either by specifying name as the
  1260. command name and optionally following it with arguments or
  1261. by using it as an option to the . built-in command.
  1262. Positional parameters are saved before each function call
  1263. and restored when completed. The arguments that follow the
  1264. function name on the calling line become positional
  1265. parameters inside the function. The return built-in can be
  1266. used to cause the function to return to the statement
  1267. following the point of invocation.
  1268. Functions can also be defined with the System V notation,
  1269. name ()
  1270. {
  1271. any shell script
  1272. BELL LABORATORIES PROPRIETARY
  1273. Not for use or disclosure outside Bell Laboratories except by
  1274. written approval of the director of the distributing organization.
  1275. - 29 -
  1276. }
  1277. Functions defined with this syntax cannot be used as the
  1278. first argument to a . procedure. ksh accepts this notation
  1279. for compatibility only. There is no need to use this
  1280. notation when writing ksh scripts.
  1281. Functions defined with the function name syntax and invoked
  1282. by name are executed in the current shell environment and
  1283. can share named variables with the calling program.
  1284. Options, other than execution trace -x, set by the calling
  1285. program are passed down to a function. The options are not
  1286. shared with the function so that any options set within a
  1287. function are restored when the function exits. Traps
  1288. ignored by the caller are ignored within the function and
  1289. cannot be enabled. Traps caught by the calling program are
  1290. reset to their default action with the function. In most
  1291. instances, the default action is to cause the function to
  1292. terminate. A trap on EXIT, defined within a function
  1293. executes after the function completes but before the caller
  1294. resumes. Therefore, any variable assignments and any
  1295. options set as part of a trap action will be effective after
  1296. the caller resumes.
  1297. By default, variables are inherited by the function and
  1298. shared by the calling program. However, for functions
  1299. defined with the function name syntax that are invoked by
  1300. name, environment substitutions preceding the function call
  1301. apply only to the scope of the function call. Also,
  1302. variables whose names do not contain a . that are defined
  1303. with the typeset built-in command are local to the function
  1304. that they are declared in. Thus, for the function defined
  1305. function name
  1306. {
  1307. typeset -i x=10
  1308. let z=x+y
  1309. print $z
  1310. }
  1311. invoked as y=13 name, x and y are local variables with
  1312. respect to the function name while z is global.
  1313. Functions defined with the name() syntax, and functions
  1314. invoked as an argument to the . command, share everything
  1315. other than positional parameters with the caller.
  1316. BELL LABORATORIES PROPRIETARY
  1317. Not for use or disclosure outside Bell Laboratories except by
  1318. written approval of the director of the distributing organization.
  1319. - 30 -
  1320. Assignments that precede the call remain in effect after the
  1321. function completes.
  1322. Alias and function names are not passed down to shell
  1323. scripts or carried across separate invocations of ksh. The
  1324. $FPATH variable gives a colon separated list of directories
  1325. that is searched for function definitions when trying to
  1326. resolve the command name. Whenever a file name contained in
  1327. $FPATH is found, the complete file is read and all functions
  1328. contained within become defined.
  1329. Calls that reference functions can be recursive. Except for
  1330. special built-ins, function names take precedence over
  1331. built-in names and names of programs when used as command
  1332. names. To write a function to replace a built-in command or
  1333. to replace a program, you must use the command built-in
  1334. command. The arguments to command are the name and
  1335. arguments of the program you want to execute. For example
  1336. to write a cd function which changes the directory and
  1337. prints out the directory name, you can write,
  1338. function cd
  1339. {
  1340. if command cd "$@"
  1341. then print -r -- $PWD
  1342. fi
  1343. }
  1344. The FPATH variable is a colon separated list that ksh uses
  1345. to search for function definitions. When ksh encounters an
  1346. autoload function, it runs the . command on the script
  1347. containing the function, and then executes the function.
  1348. Function definitions may also be placed in the ENV file.
  1349. However, this causes the shell to take longer to begin
  1350. executing.
  1351. 3.11 Process Substitution
  1352. This feature is only available on versions of the UNIX
  1353. operating system which support the /dev/fd directory for
  1354. naming open files. Each command argument of the form
  1355. <(list) or >(list) will run process list asynchronously
  1356. connected to some file in the /dev/fd directory. The name
  1357. of this file will become the argument to the command. If
  1358. BELL LABORATORIES PROPRIETARY
  1359. Not for use or disclosure outside Bell Laboratories except by
  1360. written approval of the director of the distributing organization.
  1361. - 31 -
  1362. the form with > is selected then writing on this file will
  1363. provide input for list. If < is used, then the file passed
  1364. as an argument will contain the output of the list process.
  1365. For example,
  1366. paste <(cut -f1 file1) <(cut -fB file2) | tee >(process1) >(process2)
  1367. extracts fields 1 and 3 from the files file1 and file2
  1368. respectively, places the results side by side, and sends it
  1369. to the processes process1 and process2, as well as putting
  1370. it onto the standard output. Note that the file which is
  1371. passed as an argument to the command is a UNIX system
  1372. pipe(2) so that the programs that expect to lseek(2) on the
  1373. file will not work.
  1374. 3.12 Finding Commands
  1375. The addition of aliases, functions, and more built-ins has
  1376. made it substantially more difficult to know what a given
  1377. command word really means.
  1378. There are several reasons that commands are built into the
  1379. shell rather than being separate programs. Commands that
  1380. begin with reserved words are an integral part of the shell
  1381. language itself and typically define the control flow of the
  1382. language. Some control flow commands are not reserved words
  1383. in the language but are special built-ins. Special built-
  1384. ins are built-ins that are considered a part of the language
  1385. rather than user definable commands. The best examples of
  1386. commands that fit this description are break and continue.
  1387. Because they are not reserved words, they can be the result
  1388. of shell expansions and are not effected by quoting. These
  1389. commands have the following special properties:
  1390. + Assignments that precede them apply to the current
  1391. shell process, not just to the given command.
  1392. + An error in the format of these commands cause a shell
  1393. script or function that contains them to abort.
  1394. + They cannot be overridden by shell functions.
  1395. Other commands are built-in because they perform side
  1396. effects on the current environment that would be nearly
  1397. impossible to implement otherwise. Built-ins such as cd and
  1398. read are examples of such built-ins. These built-ins are
  1399. BELL LABORATORIES PROPRIETARY
  1400. Not for use or disclosure outside Bell Laboratories except by
  1401. written approval of the director of the distributing organization.
  1402. - 32 -
  1403. semantically equivalent to commands that are not built-in
  1404. except that they don't take a path search to locate.
  1405. A third reason to have a command built-in is so that it will
  1406. be unaffected by the setting of the PATH variable. The
  1407. print command fits this category. Scripts that use print
  1408. will be portable to all sites that run ksh.
  1409. The final reason for having a command be a built-in is for
  1410. performance. On most systems it is more than an order of
  1411. magnitude faster to initiate a command that is built-in than
  1412. to create a separate process to run the command. Example
  1413. that fit this category are test and basename.
  1414. Given a command name ksh decides what it means using the
  1415. following order:
  1416. + Reserved words define commands that form part of the
  1417. shell grammar. They cannot be quoted.
  1418. + Alias substitutions occur first as part of the reading
  1419. of commands. Using quotes in the command name will
  1420. prevent alias substitutions.
  1421. + Special built-ins come next.
  1422. + Functions.
  1423. + Commands that are built-in that are not associated with
  1424. a pathname.
  1425. + If the command name contains a /, the program or script
  1426. corresponding to the given name is executed.
  1427. + A path search locates the pathname corresponding to the
  1428. command. If the pathname where it is found matches the
  1429. pathname associated with a built-in command, the
  1430. built-in command is executed. If the directory where
  1431. the command is found is listed in the FPATH variable,
  1432. the file is read into the shell like a dot script, and
  1433. a function by that name is invoked. Once a pathname is
  1434. found, ksh remembers its location and only checks
  1435. relative directories in PATH the next time the command
  1436. name is used. Assigning a value to PATH causes ksh to
  1437. forget the location of all command names.
  1438. BELL LABORATORIES PROPRIETARY
  1439. Not for use or disclosure outside Bell Laboratories except by
  1440. written approval of the director of the distributing organization.
  1441. - 33 -
  1442. + The FPATH variable is searched and files found are
  1443. treated as described above.
  1444. The first argument of the command built-in, described
  1445. earlier, skips the checks for reserved words and for
  1446. function definitions. In all other ways, command behaves
  1447. like a built-in that is not associated with a pathname. As
  1448. a result, if the first argument of command is a special
  1449. built-in, the special properties of this built-in do not
  1450. apply. For example, whereas, exec 3< foo will cause a script
  1451. containing it to abort if the open fails,
  1452. command exec 3< foo results in a non-zero exit status but
  1453. does not abort the script.
  1454. You can get a complete list of the special built-in commands
  1455. with builtin -s. In addition builtin without arguments gives
  1456. a list of the current built-ins and the pathname that they
  1457. are associated with. A built-in can be bound to another
  1458. pathname by giving the pathname for the built-in. The
  1459. basename of this path must be the name of an existing
  1460. built-in for this to succeed. Specifying the name of the
  1461. built-in without a pathname causes this built-in to be found
  1462. before a path search. On systems with run time loading of
  1463. libraries, built-in commands can be added with the builtin
  1464. command. Each command that is to be built-in must be
  1465. written as a C function whose name is of the form b_name,
  1466. where name is the name of the built-in that is to be added.
  1467. The function has the same argument calling convention as
  1468. main. The lower eight bits of the return value become the
  1469. exit status for this built-in. Builtins are added by
  1470. specifying the pathname of the library as an argument to the
  1471. -f option of builtin.
  1472. A built-in command, whence, when used with the -v option has
  1473. been provided to answer this question. A line is printed
  1474. for each argument to whence telling what would happen if
  1475. this argument were used as a command name. It reports on
  1476. reserved words, aliases, built-ins, and functions. If the
  1477. command is none of the above, it follows the path search
  1478. rules and prints the full path-name, if any, otherwise it
  1479. prints an error message.
  1480. BELL LABORATORIES PROPRIETARY
  1481. Not for use or disclosure outside Bell Laboratories except by
  1482. written approval of the director of the distributing organization.
  1483. - 34 -
  1484. 3.13 Symbolic Names
  1485. To avoid implementation dependencies, ksh accepts and
  1486. generates symbolic names for built-ins that use numerical
  1487. values in the Bourne shell. The -S option of the umask
  1488. built-in command accepts and displays default file creation
  1489. permissions symbolically. It uses the same symbolic
  1490. notation as the chmod command.
  1491. The trap and kill built-in commands allows the signal names
  1492. to be given symbolically. The names of signals and traps
  1493. corresponding to signals are the same as the signal name
  1494. with the SIG prefix removed. The trap 0 is named EXIT.
  1495. 3.14 Added Traps
  1496. A new trap named ERR has been added. This trap is invoked
  1497. whenever the shell would exit if the -e option were set.
  1498. This trap is used by Fourth Generation Make[16] which runs
  1499. ksh as a co-process.
  1500. A trap named DEBUG gets executed after each command. This
  1501. trap can be used for debugging purposes. The KEYBD trap was
  1502. described earlier.
  1503. 3.15 Debugging
  1504. The primary method for debugging Bourne shell scripts is to
  1505. use the -x option to enable the execution trace. After all
  1506. the expansions have been performed, but before each command
  1507. is executed, the trace writes to standard error the name and
  1508. arguments of each command preceded by a +. With ksh the PS4
  1509. variable is evaluated for parameter substitution and is
  1510. displayed before each command, instead of the +.
  1511. The LINENO variable is set to the current line number
  1512. relative to the beginning of the current script or function.
  1513. It is most useful as part of the PS4 prompt.
  1514. The variable RANDOM produces a random number in the range 0
  1515. to 32767 each time it is referenced. Assignment to this
  1516. variable sets the seed for the random number generator.
  1517. The parameter PPID is used to generate the process id of
  1518. the process which invoked this shell.
  1519. BELL LABORATORIES PROPRIETARY
  1520. Not for use or disclosure outside Bell Laboratories except by
  1521. written approval of the director of the distributing organization.
  1522. - 35 -
  1523. 3.16 Timing Commands
  1524. A reserved word time has been added to replace the time
  1525. command. Any function, command or pipeline can be preceded
  1526. by this reserved word to obtain information about the
  1527. elapsed, user, and system times. Since I/O redirections
  1528. bind to the command, not to time, parentheses should be used
  1529. to redirect the timing information which is normally printed
  1530. on file descriptor 2.
  1531. 4. SECURITY
  1532. There are several documented problems associated with the
  1533. security of shell procedures[17]. These security holes
  1534. occur primarily because a user can manipulate the
  1535. environment to subvert the intent of a setuid shell
  1536. procedure. Frequently, shell procedures are initiated from
  1537. binary programs, without the author's awareness, by library
  1538. routines which invoke shells to carry out their tasks. When
  1539. the binary program is run setuid then the shell procedure
  1540. runs with the permissions afforded to the owner of the
  1541. binary file.
  1542. In the Bourne shell, the IFS parameter is used to split each
  1543. word into separate command arguments. If a user knows that
  1544. some setuid program will run sh -c /bin/pwd (or any other
  1545. command in /bin) then the user sets and exports IFS=/.
  1546. Instead of running /bin/pwd the shell will run bin with pwd
  1547. as an argument. The user puts his or her own bin program
  1548. into the current directory. This program can create a copy
  1549. of the shell, make this shell setuid, and then run the
  1550. /bin/pwd program so that the original program continues to
  1551. run successfully. This kind of penetration is not possible
  1552. with ksh since the IFS parameter only splits arguments that
  1553. result from command or parameter substitution.
  1554. Some setuid programs run programs using system() without
  1555. giving the full pathname. If the user sets the PATH
  1556. variable so that the desired command will be found in his or
  1557. her local bin, then the same technique described above can
  1558. be employed to compromise the security of the system. To
  1559. close up this and other security holes, ksh resets the
  1560. effective user id to the real user id and the effective
  1561. group id to the real group id unless the privileged option
  1562. (-p) is specified at invocation. In this mode, the
  1563. BELL LABORATORIES PROPRIETARY
  1564. Not for use or disclosure outside Bell Laboratories except by
  1565. written approval of the director of the distributing organization.
  1566. - 36 -
  1567. privileged mode, the .profile and ENV files are not
  1568. processed. Instead, the file /etc/suid_profile is read and
  1569. executed. This gives an administrator control over the
  1570. environment to set the PATH variable or to log setuid shell
  1571. invocations. Clearly security of the system is compromised
  1572. if /etc or this file is publicly writable.
  1573. In the Berkeley UNIX version the operating system looks for
  1574. the characters #! as the first two characters of an
  1575. executable file. If these characters are found, then the
  1576. next word on this line is taken as the interpreter to invoke
  1577. for this command and the interpreter is execed with the name
  1578. of the script as argument zero and argument one. If the
  1579. setuid or setgid bits are on for this file, then the
  1580. interpreter is run with the effective uid and/or gid set
  1581. accordingly. This scheme has two major drawbacks. First of
  1582. all, using the #! notation forces an exec of the
  1583. interpreter even when the call is invoked from the
  1584. interpreter which it must exec. This is inefficient since
  1585. the interpreter can handle a failed exec much faster than
  1586. starting up again. More importantly, setuid and setgid
  1587. procedures provide an easy target for intrusion. By linking
  1588. a setuid or setgid procedure to a name beginning with a -
  1589. the interpreter is fooled into thinking that it is being
  1590. invoked with a command line option rather than the name of a
  1591. file. When the interpreter is the shell, the user gets a
  1592. privileged interactive shell. There is code in ksh to guard
  1593. against this simple form of intrusion.
  1594. A more reliable way to handle setuid and setgid procedures
  1595. is provided with ksh. The technique does not require any
  1596. changes to the operating system and provides better
  1597. security. Another advantage to this method is that it also
  1598. allows scripts which have execute permission but no read
  1599. permission to run. Taking away read permission makes
  1600. scripts more secure.
  1601. The method relies on a setuid root program to authenticate
  1602. the request and exec the shell with the correct mode bits to
  1603. carry out the task. This shell is invoked with the
  1604. requested file already open for reading. A script which
  1605. cannot be opened for reading or which has its setuid and/or
  1606. setgid bits turned on causes this setuid root program to get
  1607. execed. For security reasons, this program is given the
  1608. full pathname /etc/suid_exec. A description of the
  1609. implementation of the /etc/suid_exec program can be found in
  1610. BELL LABORATORIES PROPRIETARY
  1611. Not for use or disclosure outside Bell Laboratories except by
  1612. written approval of the director of the distributing organization.
  1613. - 37 -
  1614. a separate paper[18].
  1615. 5. CODE CHANGES
  1616. ksh is written in ANSI-C as a reusable library. The code
  1617. can be compiled with C++ and older K&R C as well. The code
  1618. uses the IEEE POSIX 1003.1 and ISO 9945-1 standard[19]
  1619. wherever possible so that ksh should be able to run on any
  1620. POSIX compliant system. In addition, it is possible to
  1621. compile ksh for older systems.
  1622. Unlike earlier version of the Bourne shell, ksh treats eight
  1623. bit characters transparently without stripping off the
  1624. leading bit. There is also a compile time switch to enable
  1625. handling multi-byte and multi-width characters sets.
  1626. On systems with dynamic libraries, it is possible to add
  1627. built-in commands at run time with the built-in command
  1628. builtin described earlier. It is also possible to embed ksh
  1629. in applications in a manner analogous to tcl.
  1630. 6. EXAMPLE
  1631. An example of a ksh script is included in the Appendix.
  1632. This one page program is a variant of the UNIX system
  1633. grep(1) program. Pattern matching for this version of grep
  1634. means shell patterns.
  1635. The first half uses the getopts command to find the option
  1636. flags. Nearly all options have been implemented. The
  1637. second half goes through each line of each file to look for
  1638. a pattern match.
  1639. This program is not intended to serve as a replacement for
  1640. grep which has been highly tuned for performance. It does
  1641. illustrate the programming power of ksh. Note that no
  1642. auxiliary processes are spawned by this script. It was
  1643. written and debugged in under two hours. While performance
  1644. is acceptable for small files, this program runs at only one
  1645. tenth the speed of grep for large files.
  1646. BELL LABORATORIES PROPRIETARY
  1647. Not for use or disclosure outside Bell Laboratories except by
  1648. written approval of the director of the distributing organization.
  1649. - 38 -
  1650. 7. PERFORMANCE
  1651. ksh executes many scripts faster than the System V Bourne
  1652. shell; in some cases more than 10 times as fast. The
  1653. primary reason for this is that ksh creates fewer processes.
  1654. The time to execute a built-in command or a function is one
  1655. or two orders of magnitude faster than performing a fork()
  1656. and exec() to create a separate process. Command
  1657. substitution and commands inside parentheses are performed
  1658. without creating another process, unless necessary to
  1659. preserve correct behavior.
  1660. Another reason for improved performance is the use of the
  1661. sfio[20], library for I/O. The sfio library buffers all I/O
  1662. and output and buffers are flushed only when required. The
  1663. algorithms used in sfio perform better than traditional
  1664. versions of standard I/O so that programs that spend most of
  1665. their time formatting output may actually perform better
  1666. than versions written in C.
  1667. Several of the internal algorithms have been changed so that
  1668. the number of subroutine calls has been substantially
  1669. reduced. ksh uses variable sized hash tables for variables.
  1670. Scripts that rely heavily on referencing variables execute
  1671. faster. More processing is performed while reading the
  1672. script so that execution time is saved while running loops.
  1673. These changes are not noticeable for scripts that fork() and
  1674. run processes, but they reduce the time that it takes to
  1675. interpret commands by more than a factor of two.
  1676. Most importantly, ksh provide mechanisms to write
  1677. applications that do not require as many processes. The
  1678. arithmetic provided by the shell eliminates the need for the
  1679. expr command. The pattern matching and substring
  1680. capabilities eliminate the need to use sed or awk to process
  1681. strings.
  1682. The architecture of ksh makes it easy to make commands
  1683. built-ins without changing the semantics at all. Systems
  1684. that have run-time binding of libraries allow applications
  1685. to be sped up by supplying the critical programs as shell
  1686. built-in commands. Implementations on other systems can add
  1687. built-in commands at compile time.
  1688. BELL LABORATORIES PROPRIETARY
  1689. Not for use or disclosure outside Bell Laboratories except by
  1690. written approval of the director of the distributing organization.
  1691. - 39 -
  1692. 8. CONCLUSION
  1693. The 1988 version of ksh has tens of thousands of regular
  1694. users and is a suitable replacement for the Bourne shell.
  1695. The 1993 version of ksh is essentially upward compatible
  1696. with both the 1988 version of ksh and with the recent IEEE
  1697. POSIX and ISO shell standard. The 1993 version offers many
  1698. advantages for programming applications, and it has been
  1699. rewritten so that it can be used in embedded applications.
  1700. It also offers improved performance.
  1701. MH-11267-DGK-dgk David G. Korn
  1702. BELL LABORATORIES PROPRIETARY
  1703. Not for use or disclosure outside Bell Laboratories except by
  1704. written approval of the director of the distributing organization.
  1705. - 40 -
  1706. APPENDIX
  1707. BELL LABORATORIES PROPRIETARY
  1708. Not for use or disclosure outside Bell Laboratories except by
  1709. written approval of the director of the distributing organization.
  1710. - 41 -
  1711. References
  1712. 1. S. R. Bourne, An Introduction to the UNIX Shell," BSTJ -
  1713. Vol. 57, No. 6 part 2, pages 1947-1972.
  1714. 2. POSIX - Part 2: Shell and Utilities, IEEE Std 1003.2-
  1715. 1992, ISO/IEC 9945-2:1992.
  1716. 3. Al Aho, Brian Kernighan, and Peter Weinberger, The AWK
  1717. Programming Language, Addison Wesley, 1988.
  1718. 4. LLoyd H. Nakatani and Laurence W. Ruedisueli, The FIT
  1719. Programming Language Primer, TN 1126-920301-03, 1992.
  1720. 5. Larry Wall, The PERL Programming Language,
  1721. 6. John K. Ousterhout, Tcl: An Embeddable Command Language,
  1722. Proceedings of the USENIX meeting, pp. ?-?, 1990.
  1723. 7. S. R. Bourne, An Introduction to the UNIX Shell, Bell
  1724. System Technical Journal, Vol. 57, No. 6, Part 2, pp.
  1725. 1947-1972, July 1978.
  1726. 8. W. Joy, An Introduction to the C Shell, Unix
  1727. Programmer's Manual, Berkeley Software Distribution,
  1728. University of California, Berkeley, 1980.
  1729. 9. Morris Bolsky and David Korn, The KornShell Command and
  1730. Programming Language, Prentice Hall, 1989.
  1731. 10. Jason Levitt, The Korn Shell: An Emerging Standard,
  1732. UNIX/World, pp. 74-81, September 1986.
  1733. 11. Rich Bilancia, Proficiency and Power are Yours With the
  1734. Korn Shell, UNIX/World, pp. 103-107, September 1987.
  1735. 12. John Sebes, Comparing UNIX Shells, UNIX Papers, Edited
  1736. by the Waite Group, Howard W. Sams & Co., 1987.
  1737. 13. T. A. Dolotta and J. R. Mashey, Using the shell as a
  1738. Primary Programming Tool, Proc. 2nd. Int. Conf. on
  1739. Software Engineering, 1976, pages 169-176.
  1740. BELL LABORATORIES PROPRIETARY
  1741. Not for use or disclosure outside Bell Laboratories except by
  1742. written approval of the director of the distributing organization.
  1743. - 42 -
  1744. 14. J. S. Pendergrast, WKSH - Korn Shell with X-Windows
  1745. Support, USL.
  1746. 15. American National Standard for Information Systems -
  1747. Programming Language - C, ANSI X3.159-1989.
  1748. 16. G. S. Fowler, "The Fourth Generation Make," Proceedings
  1749. of the Portland USENIX meeting, pp. 159-174, 1985.
  1750. 17. F. T. Grampp and R. H. Morris, UNIX Operating System
  1751. Security, AT&T Bell Labs Tech. Journal, Vol. 63, No. 8,
  1752. Part 2, pp.1649-1671, 1984.
  1753. 18. D. G Korn Parlez-vous Kanji? TM-59554-860602-03, 1986.
  1754. 19. POSIX - Part 1: System Application Program Interface,
  1755. IEEE Std 1003.1-1990, ISO/IEC 9945-1:1990.
  1756. 20. David Korn and Kiem-Phong Vo, SFIO - A Safe/Fast
  1757. Input/Output library, Proceedings of the Summer Usenix,
  1758. pp. , 1991.
  1759. BELL LABORATORIES PROPRIETARY
  1760. Not for use or disclosure outside Bell Laboratories except by
  1761. written approval of the director of the distributing organization.