faq.html 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  2. <HTML>
  3. <HEAD>
  4. <META name="generator" content="mm2html (AT&amp;T Research) 2012-01-11">
  5. <TITLE> general shell </TITLE>
  6. <META name="author" content="gsf+dgk+kpv">
  7. <SCRIPT type="text/javascript">
  8. var A, P, Q;
  9. function FAQ_mark(mark) {
  10. P = null;
  11. for (var i = 0; i < A.length; i++)
  12. A[i].className = mark;
  13. }
  14. function FAQ_init() {
  15. Q = document.getElementsByTagName("dt");
  16. A = document.getElementsByTagName("dd");
  17. FAQ_mark("hide");
  18. for (var i = 0; i < Q.length; i++) {
  19. Q[i].onclick=function() {
  20. var next = this.nextSibling;
  21. while (next.nodeType != 1)
  22. next = next.nextSibling;
  23. if (P != null && P != next)
  24. P.className = "hide";
  25. if (next.className == "hide") {
  26. next.className = "show";
  27. P = next;
  28. }
  29. else {
  30. next.className = "hide";
  31. P = null;
  32. }
  33. }
  34. }
  35. }
  36. window.onload = FAQ_init;
  37. </SCRIPT>
  38. <STYLE type="text/css">
  39. div.FI { padding-left:2em; text-indent:0em; }
  40. div.HI { padding-left:4em; text-indent:-2em; }
  41. dt { margin: 15px 40px 5px; cursor: pointer; }
  42. dt:before {
  43. content: "Q";
  44. font-family: Georgia, "Times New Roman", Times, serif;
  45. margin-right: 7px;
  46. padding: 2px 6px 5px;
  47. color: #FFD87D;
  48. background-color: teal;
  49. font-weight: normal;
  50. margin-left: -35px;
  51. position: relative;
  52. }
  53. dd { margin: 25px 70px 0px; }
  54. li { padding: 2px 0; }
  55. .show { display: block; }
  56. .hide { display: none; }
  57. </STYLE>
  58. </HEAD>
  59. <BODY bgcolor=white link=slateblue vlink=teal >
  60. <TABLE border=0 align=center width=96%>
  61. <TBODY><TR><TD valign=top align=left>
  62. <!--INDEX--><!--/INDEX-->
  63. <P>
  64. <P><CENTER><FONT color=red><FONT face=courier><H3><A name="general">general</A></H3></FONT></FONT></CENTER>
  65. <DIV class=SH>
  66. <DL>
  67. <DT>What is KornShell?<DD>
  68. KornShell is a command and scripting language that is a superset of the System V UNIX shell,
  69. aka, BourneShell (or
  70. <EM>sh</EM>).
  71. <DT>What is ksh?<DD>
  72. ksh is the name of the program that implements the KornShell language.
  73. <DT>What is the history of ksh?<DD>
  74. ksh was written by David Korn at Bell Telephone Laboratories.
  75. David Korn is currently at AT&amp;T Research.
  76. The first version of ksh was in 1983.
  77. It was the first shell to have command line editing with both emacs and vi style interaction.
  78. The 1986 version was the first to offer multibyte support.
  79. The 1988 version of ksh is the version that was adopted by System V Release 4 UNIX
  80. and was a source document for the IEEE POSIX and ISO shell standards.
  81. The 1993 version is a major rewrite of the 1988 version and focuses more on scripting.
  82. <DT>Where is the official description of the KornShell language?<DD>
  83. The Bolsky and Korn book,
  84. <EM>The KornShell Command and Programming Language</EM>,
  85. published by Prentice Hall, defines the 1988 version.
  86. The newer Bolsky and Korn book,
  87. <EM>The New KornShell Command and Programming Language</EM>,
  88. also published by Prentice Hall, describes the 1993 version.
  89. There are many new features since this book was published and
  90. the man page for ksh93 is kept up to date.
  91. <DT>What are the major new features of KornShell 1993?<DD>
  92. The only major new interactive features are key binding and tab completion.
  93. Major new language features are floating point arithmetic,
  94. associative arrays, complete ANSI-C printf, name reference
  95. variables, new expansion operators, dynamic loading of
  96. built-in commands, active variables, and compound variables.
  97. Active and compound variables allow shell variables to
  98. behave like objects. The ability to define types was added in 2009.
  99. In addition, ksh93 has been written to be
  100. extensible with an C language API for programming extensions.
  101. <DT>Are any further releases of ksh planned?<DD>
  102. Yes, the KornShell language and ksh implementation are in active development.
  103. Most of the focus will be on scripting and reusability.
  104. <DT>Why are newer release of ksh still called ksh93?<DD>
  105. We started the AST/ksh OpenSource release process in the late 90's.
  106. At that point ksh93 was the well-known name for ksh.
  107. The OpenSource release was finally granted in March 2000.
  108. No one has since volunteered to repeat that process for ksh<EM>XX</EM>.
  109. <DT>How can I determine the release or version of a particular ksh?<DD>
  110. The current version and release string may be accessed by
  111. <STRONG>${.sh.version}</STRONG>
  112. and
  113. <STRONG>$KSH_VERSION</STRONG>.
  114. The format is <STRONG>Version</STRONG> <EM>features</EM> 93<EM>version</EM>&#0091;-/+&#0093; <EM>release</EM>:
  115. <UL type=square>
  116. <LI>
  117. <EM>features</EM> --
  118. compile time features, typically enabled by <STRONG>SHOPT_</STRONG><EM>foo</EM> state variables in the
  119. ksh93 Makefile.
  120. A single letter represents each feature:
  121. <UL type=circle>
  122. <LI>
  123. <STRONG>A</STRONG>
  124. (<EM>SHOPT_AUDIT</EM>)
  125. <LI>
  126. <STRONG>B</STRONG>
  127. (<EM>SHOPT_BASH</EM>)
  128. bash compatibility mode.
  129. <LI>
  130. <STRONG>J</STRONG>
  131. (<EM>SHOPT_COSHELL</EM>)
  132. <STRONG>-lcoshell</STRONG> job pools.
  133. <LI>
  134. <STRONG>j</STRONG>
  135. (<EM>SHOPT_BGX</EM>)
  136. <LI>
  137. <STRONG>L</STRONG>
  138. (<EM>SHOPT_ACCT</EM>)
  139. <LI>
  140. <STRONG>M</STRONG>
  141. (<EM>SHOPT_MULTIBYTE</EM>)
  142. <LI>
  143. <STRONG>P</STRONG>
  144. (<EM>SHOPT_PFSH</EM>)
  145. <LI>
  146. <STRONG>R</STRONG>
  147. (<EM>SHOPT_REGRESS</EM>)
  148. </UL>
  149. <LI>
  150. <EM>version</EM>--
  151. a lower-case letter signifying major release points.
  152. An optional <STRONG>-</STRONG> following <EM>features</EM> signifies an alpha release.
  153. The first stable release has no <STRONG>-</STRONG>.
  154. An optional <STRONG>+</STRONG> signifies a stable release with bug patches and minor enhancements.
  155. <LI>
  156. <EM>release</EM>--
  157. the release date in <EM>YYYY-MM-DD</EM> form.
  158. This date corresponds to AST package and git repository releases.
  159. </UL>
  160. <STRONG>KSH_VERSION</STRONG>
  161. in a numeric context is an integer that encodes the release
  162. <EM>YYYYMMDD</EM>.
  163. <DT>What new features are planned for ksh?<DD>
  164. We are in the early stage of planning but the likely additions
  165. are namespaces, ability to read xml and json object into shell variables,
  166. and handling of queued signals.
  167. Support for multi-threading is also being considered.
  168. <DT>Is KornShell public domain?<DD>
  169. Yes, the language description is public domain and
  170. can be reimplemented.
  171. Some of the KornShell language features have been reimplemented
  172. in the GNU shell, bash, in zsh and mksh, and in pdksh, a public domain
  173. implementation.
  174. <DT>Is ksh public domain?<DD>
  175. No, earlier versions were owned by both AT&amp;T and Novell.
  176. The 1993 version is owned by both Lucent and AT&amp;T.
  177. <DT>Is source code available?<DD>
  178. Starting in March 2000, the ksh93 source is available
  179. as part of a larger collection of software called
  180. the ast-open software package which can be downloaded
  181. from the
  182. <A href="https://github.com/att/ast" target=_top>github page.</A>
  183. <DT>What are the licensing terms?<DD>
  184. The exact license terms can be found on the
  185. <A href="https://github.com/att/ast/blob/master/LICENSE.md" target=_top>licence page.</A>
  186. <DT>Does the license allow binaries to be freely redistributed?<DD>
  187. Yes, provided you make the license terms available to
  188. everyone you distribute binaries to.
  189. <DT>If I make changes to the code, do I have to make them public?<DD>
  190. No, you do not have to make them public.
  191. However, if you distribute the changes, you must allow us to be able
  192. to get these changes and distribute them along with the source.
  193. <DT>Why do some vendors still ship ksh88, not ksh93?<DD>
  194. Since ksh88 was included in System V release 4, most vendors
  195. have just included this version. However most Linux systems
  196. and Mac OS provide ksh93 version 's' or later. Solaris11 uses
  197. ksh93 as /bin/sh.
  198. <DT>Do you provide support for ksh?<DD>
  199. No, we will try to fix any bugs we hear about in future
  200. releases, but we do not provide any official support.
  201. <DT>Is ksh supported commercially?<DD>
  202. Software vendors that supply ksh with
  203. their systems typically support it for that system.
  204. <DT>What is pdksh and is it related to ksh or KornShell?<DD>
  205. pdksh is a public domain version of a UNIX shell that is
  206. unrelated to ksh.
  207. It supports most of the 1988 KornShell language features and some of the 1993 features.
  208. Some KornShell scripts will not run with pdksh.
  209. <DT>How is the MKS Toolkit KornShell related to KornShell?<DD>
  210. MKS Toolkit KornShell is a completely independent implementation
  211. that supports a subset of the 1988 KornShell language.
  212. <DT>What systems does ksh run on?<DD>
  213. ksh has been written to be portable.
  214. It has been ported to virtually run on every known UNIX system.
  215. In addition it runs on non-UNIX systems such as IBM's MVS using OpenEdition, and
  216. Microsoft's Windows 9X, Windows NT and Windows 2000.
  217. ksh is part of the UWIN (Unix for Windows)</A>
  218. software,
  219. <DT>Does ksh conform to the IEEE POSIX and ISO shell standard?<DD>
  220. The 1993 version should conform to the 1992 standard.
  221. At one point it had passed the test suite created by X/OPEN.
  222. <DT>Will KornShell 88 scripts run with KornShell 93?<DD>
  223. In almost all cases, the answer is yes.
  224. However, the IEEE POSIX and ISO standards required a few
  225. changes that could cause scripts to fail.
  226. There is a separate document that lists all known incompatibilities.
  227. <DT>Can ksh run as /bin/sh?<DD>
  228. We have installed ksh as /bin/sh on several systems without
  229. encountering any problems. It is /bin/sh on Solaris11.
  230. Our Linux systems use this instead of bash.
  231. </DL>
  232. </DIV>
  233. <P>
  234. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="interactive">interactive</A></H3></FONT></FONT></CENTER>
  235. <DIV class=SH>
  236. <DL>
  237. <DT>How do I get separate history files for shell?<DD>
  238. ksh uses a shared history file for all shells that
  239. use the same history file name.
  240. This means that commands entered in one window will be seen by
  241. shells in other windows.
  242. To get separate windows, the HISTFILE variable needs to be set to different name
  243. before the first history command is created.
  244. <DT>How do I get the time of day in my prompt?<DD>
  245. You can use printf with supports the %T format for time and date formatting.
  246. For example, the format %(%H:%M:%S)T specifies time in hour, minute, second
  247. format and if no argument is specified, the current time is used. Thus setting
  248. PS1='$(printf "%(%H:%M:%S)T" $' will output the time of day before the
  249. $ prompt.
  250. <DT>Why does the screen width not function correctly when non-printing characters are in my prompt?<DD>
  251. The shell computes the screen width by subtracting the width of the prompt from the screen width.
  252. To account for non-printing characters, for example escape sequences that display in the title
  253. bar, follow these characters with a carriage return.
  254. The shell starts recomputing the width after each carriage return.
  255. <DT>What is the PS4 prompt and how is it used?<DD>
  256. The PS4 prompt is evaluated and displayed before each line when
  257. running an execution trace.
  258. If unset, a + and a &lt;space&gt; will
  259. be output before each line in the trace.
  260. Putting '$LINENO' inside PS4 will cause the line number to be displayed.
  261. Putting '$SECONDS' in the PS4 prompt will cause the elapsed time
  262. to be displayed before each line.
  263. Note that single quotes are used to prevent the expansion from happening
  264. when PS4 is defined.
  265. <DT>How is keybinding done?<DD>
  266. ksh93 provides a KEYBD trap that gets executed whenever a key
  267. is entered from the keyboard.
  268. Using this trap, and the associative
  269. array feature of ksh93, a keybind function can easily be written
  270. which will map any entered key sequence to another key sequence.
  271. <DT>How do I get the arrow keys to work?<DD>
  272. Starting with the 'h' point release, on most keyboards you
  273. do not have to do anything to get the arrow keys to work.
  274. However, if they do not generate standard escape sequences,
  275. then you will have to use a keybinding function to get them
  276. to work.
  277. <DT>Does ksh support file name completion?<DD>
  278. Yes, it does.
  279. The default key binding is &lt;ESC&gt;&lt;ESC&gt;
  280. however, starting with the 'g' point release, &lt;TAB&gt; also works
  281. for completion.
  282. <DT>Does ksh support command completion?<DD>
  283. If you perform completion on the first word of a command,
  284. ksh will do completion using aliases, functions, and commands.
  285. <DT>Is completion programmable?<DD>
  286. Yes, using the key binding mechanism, you can script the behavior
  287. of any key and therefore cause the current contents of any
  288. line to be replaced by any other line.
  289. <DT>Is there any way to get the command-line editor to go to more than a single line?<DD>
  290. The multiline option (now on by default) allows lines longer than the width
  291. of the screen to be displayed on multiple lines on the screen.
  292. Also in vi-mode, if you hit 'v' while in control mode, it will bring
  293. up a full screen version of vi on the current command.
  294. The command
  295. will execute when you exit vi.
  296. <DT>What is predictive editing?<DD>
  297. In 2010, a compile option was added that cause the shell to try to predict
  298. what you were trying to type by looking in the history file for all lines
  299. that matched and presenting them as a menu. Any line starting with # would
  300. use the characters you type to find matching lines from the history file.
  301. If you find the line you wanted, you can enter the number followed by
  302. &lt;TAB&gt; or newline. However bugs in earlier version led to core dumps.
  303. <DT>Can I use the shell line editor on other commands?<DD>
  304. The command ie, that comes along with shell, can be used
  305. to run line input oriented commands with command line editing.
  306. <DT>When I do echo $?, I am getting 267. What does this mean?<DD>
  307. ksh93 reports process that terminate with a signal as 256+signo.
  308. Earlier versions used 128+signo but this makes it impossible
  309. to distinguish from a command exit with that value.
  310. If you run
  311. <DIV class=FI>
  312. <PRE>
  313. kill -l $?
  314. </DIV>
  315. </PRE>
  316. on this signal number, it will give the the name of the signal
  317. that caused this exit.
  318. <DT>When I type builtin, I notice that some of these are full pathnames. What does this mean?<DD>
  319. Builtins that are not bound to pathnames are always searched
  320. for before doing a path search.
  321. Builtins that are bound
  322. to pathnames are only executed when the path search would
  323. bind to this pathname.
  324. <DT>What is a self generating man page?<DD>
  325. A self generating man page is one that is generated by the
  326. option parser within that command using an extended version
  327. of the getopts function.
  328. The man page can be generated in html,
  329. troff, or directly for the terminal.
  330. Most builtin commands
  331. in the shell have self generating man pages so that you
  332. can run for example,
  333. <STRONG>kill &#45;&#45;man</STRONG>
  334. or
  335. <STRONG>kill &#45;&#45;html</STRONG>
  336. to get
  337. the description of kill to the screen or as an html file.
  338. All self-documenting output is to the standard error,
  339. so you must redirect 2&gt;... to capture the output.
  340. <P>
  341. This same method can also be used for shell scripts.
  342. Run
  343. <STRONG>getopts --man</STRONG>
  344. for more details.
  345. <DT>What is autoloading?<DD>
  346. Autoloading was a method used in ksh88, and still permitted in ksh93
  347. to declare that a name corresponded to a function.
  348. The function
  349. would be loaded and executed when first referenced.
  350. This was
  351. necessary since FPATH was always searched after PATH with ksh88
  352. and therefore if you defined a function whose name was the same
  353. as that of a program on your path, the program on your path
  354. would have been executed.
  355. With ksh93, when a pathname is
  356. encountered that is on PATH, but also is in FPATH, this directory
  357. is assumed to be a function directory.
  358. Thus, you can have
  359. function directories searched before program directories so
  360. that autoloading is no longer needed.
  361. <DT>Why does the output from 'time command 2&gt; file' come out on the screen?<DD>
  362. The time command is a compound command in ksh and time is a reserved
  363. word It can be followed by any pipeline. Thus, redirections applied
  364. at the end are for the command, not to time itself. You can use
  365. time {...;} 2&gt; file to capture the timing output to a file. Note,
  366. that with ksh, time works with all commands, for example,
  367. time for i; do xxx;done.
  368. <DT>When I run 'mv * ../elsewhere' I so that get '-ksh: mv: cannot execute &#0091;Arg list too long&#0093;', what causes this?<DD>
  369. UNIX systems have a limit to the space consumed by command arguments and
  370. environment variables when running commands that are not built into
  371. the shell. The configuration parameter ARG_MAX defines this limit.
  372. You can run 'getconf ARG_MAX' to find the limit for your system. Note
  373. that the shell expands * to the list of files in the current directory
  374. before running mv. In many case the xargs or tw command can be
  375. used to work around this problem by splitting the line into chunks
  376. and invoking the command. Another way to work around this limit
  377. is to make the command a builtin. On systems in which the cmd
  378. library is installed, you can invoke 'builtin &#45;f cmd mv' to make
  379. mv a shell builtin in which case the line length limit no longer
  380. applies. Another alternative is to use a for loop and invoke
  381. the mv command for each file, for exampe,
  382. 'for i in *;do mv $i ../elsewhere;done'.
  383. Starting with ksh93o+, a new feature was added to ksh to overcome
  384. this limit in some cases. If a command is preceded by
  385. 'command &#45;x', and it fails because there are two many arguments,
  386. the command will be run multiple times with subsets of the arguments.
  387. However, the change in ksh93o+ does not work in the above case
  388. because the ../elsewhere is not used for each subset. This problem
  389. was resolved starting in ksh93p so that
  390. command &#45;x mv * ../elsewhere
  391. should work. Note that it is possible to do alias mv='command &#45;x mv'
  392. <DT>Is there any way to generate the list of .c files in the current directory and all the subdirectories?<DD>
  393. Starting with ksh93o+, the globstar option (set &#45;G or set &#45;o globstar)
  394. was added. With globstar enabled, ** by itself matches zero or more
  395. directories or files, and **/ matches zero or more directories so that
  396. **/*.c will match all .c files under the current directory.
  397. <DT>Is there any way to prevent sending a HUP signal to a job when I log out if I didn't nohup the job?<DD>
  398. Yes, the disown command tells ksh not to forward the HUP signal
  399. to the specified jobs when it disconnects.
  400. </DL>
  401. </DIV>
  402. <P>
  403. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="programming">programming</A></H3></FONT></FONT></CENTER>
  404. <DIV class=SH>
  405. <DL>
  406. <DT>What is the difference between * and @, for example, and ?<DD>
  407. When used outside of "", they are equivalent.
  408. However, within
  409. double quotes, "$@" produces one argument for each positional
  410. parameter, and "$* produces a single argument.
  411. Note that "$@"
  412. preserves arguments lists, whereas $* may not unless both
  413. word splitting and pathname expansion are disabled.
  414. <DT>Why do I need spaces around { and } but not around ( and )?<DD>
  415. The characters ( and ) are shell metacharacters and are always
  416. treated specially.
  417. For historical reasons, { and } were
  418. treated as reserved words and are only special as separate
  419. words at locations in which a command can begin.
  420. <DT>How do I get read to maintain the &#0092; characters?<DD>
  421. Use read -r instead.
  422. <DT>How can I a write a ksh script that responds directly to each character so that you user just has to enter y, not y&lt;return&gt;?<DD>
  423. There are two ways to do this.
  424. The easiest is to use
  425. <DIV class=FI>
  426. <PRE>
  427. read -n1 x
  428. </DIV>
  429. </PRE>
  430. Alternatively, you could do
  431. <DIV class=FI>
  432. <PRE>
  433. function keytrap
  434. {
  435. .sh.edchar=${sh.edchar}$'
  436. }
  437. trap keytrap KEYBD
  438. </DIV>
  439. </PRE>
  440. and then
  441. <DIV class=FI>
  442. <PRE>
  443. read x
  444. </DIV>
  445. </PRE>
  446. <DT>What is the purpose of $'...'?<DD>
  447. The $'...' string literal syntax was added to ksh93 to solve the problem
  448. of entering special characters in scripts.
  449. It uses
  450. ANSI-C rules to translate the string between the '...'.
  451. It would have been cleaner to have all "..." strings handle
  452. ANSI-C escapes, but that would not be backward compatible.
  453. <DT>What is the -n option used for?<DD>
  454. You should always run ksh -n on each script you write.
  455. The -n
  456. option will check for syntax errors on paths that might not
  457. even be checked when you run the script.
  458. It also produces
  459. a number of warning messages.
  460. <DT>Why are both `...` and $(...) used for command substitution?<DD>
  461. The `...` method has some rather strange quoting rules
  462. and does not nest easily.
  463. $(...) was added to ksh88 to
  464. make command substitution easy to use.
  465. `...` is provided
  466. for backwards compatibility only.
  467. <DT>How can I tell if all the commands of a pipeline have succeeded?<DD>
  468. The pipefail option was added to the 'g' point release of ksh93.
  469. With pipefail set, a pipeline will fail if any element of the
  470. pipeline fails.
  471. The exit status will be that of the first
  472. command that has failed.
  473. <DT>What is the difference between &#0091;...&#0093; and &#0091;&#0091;...&#0093;&#0093;?<DD>
  474. The &#0091;&#0091;...&#0093;&#0093; is processed as part of the shell grammar
  475. whereas &#0091;...&#0093; is processed like any other command.
  476. Operators and operands are detected when the command is
  477. read, not after expansions are performed.
  478. The shell does not
  479. do word splitting or pathname generation inside &#0091;&#0091;...&#0093;&#0093;.
  480. This allows patterns to be specified for string matching
  481. purposes. You should use &#0091;&#0091;...&#0093;&#0093; instead of &#0091;...&#0093; and test.
  482. <DT>How come &#0091;&#0091; $foo == $bar &#0093;&#0093; is true and &#0091;&#0091; $bar == $foo &#0093;&#0093; is false?<DD>
  483. The == operator is not symmetrical.
  484. It takes a string on the left
  485. and a pattern on the right.
  486. However, if you double quote the right
  487. hand side, which removes the special meaning of pattern match
  488. characters, then this becomes a string comparison so that
  489. &#0091;&#0091; "$foo" == "bar" &#0093;&#0093; and &#0091;&#0091; "$bar" == "$foo" &#0093;&#0093; are equivalent.
  490. <DT>Why does ksh93 have print since echo already exists is is widely used?<DD>
  491. The behavior of echo varies from system to system.
  492. The POSIX standard does not define the behavior of echo when
  493. the first argument beings with a - or when any argument
  494. contains a &nbsp;character.
  495. This makes echo pretty useless for
  496. use in portable scripts.
  497. <DT>What is $bar after running 'echo foo | read bar'?<DD>
  498. The answer is foo.
  499. ksh runs the last component of a pipeline
  500. in the current process.
  501. Some shells run it as a subshell
  502. as if you had invoked it as echo foo | (read bar).
  503. <DT>How can I access a substring of a variable?<DD>
  504. The syntax ${varname:offset:len} can be used to generate
  505. the string of length len starting at the the specified
  506. offset. String offsets start at 0. If :len is omitted,
  507. then the remainder of the string will be used. Both offset
  508. and len can be arithmetic expressions. A negative offset is
  509. subtracted from the last offset.
  510. <DT>What is the difference between ((expr)) and $((expr))?<DD>
  511. ((expr)) is a command that evaluates an arithmetic expression.
  512. The exit status of this command is 0 if the expression
  513. evaluates to non-zero and is 1 if it evaluates to 0.
  514. 0 is an string expansion that expands to a string
  515. representation of the value of this arithmetic expression.
  516. It can be used anywhere a variable substitution is permitted.
  517. <P>
  518. <DT>What is the difference between $((x*y)) and $(($x*$y))?<DD>
  519. In the first case the value of x and the value of y are multiplied
  520. together, and then their result is converted to a string.
  521. In the
  522. second case variables $x, *, and $y are concatenated to form
  523. an arithmetic expression which is then evaluated.
  524. This can
  525. yield different results, for example,
  526. <DIV class=FI>
  527. <PRE>
  528. x=2+3 y=4+5
  529. print $((x*y)) \$(($x*$y))
  530. 45 19
  531. When x and y are numeric the first form is recommended for better
  532. performance.
  533. </DIV>
  534. </PRE>
  535. <DT>How do I handle filenames with spaces in them?<DD>
  536. To be POSIX conforming, ksh has to do word splitting and
  537. pathname expansion the results of substitutions.
  538. You can
  539. enclose variable substitutions in "..." to prevent both
  540. word splitting and pathname expansion.
  541. Alternatively,
  542. you can disable word splitting by setting IFS='' and
  543. pathname generation with set -o noglob.
  544. <DT>What are active variables?<DD>
  545. By default shell variables are passive.
  546. They hold values
  547. given to them on assignment, and return values on reference.
  548. Active variables allow the assignment and reference (and
  549. other actions) be controlled by functions specific to that
  550. variable.
  551. At the shell level, a 'get', 'set', or 'unset'
  552. shell function can be defined for any variable to make them
  553. active, so that the function foo.set will be invoked whenever
  554. the variable foo is assigned a value.
  555. At the C interface
  556. level, several functions can be stacked together for an
  557. active variable.
  558. <DT>What is the difference between function name and name()?<DD>
  559. In ksh88 these were the same.
  560. However, the POSIX standard
  561. choose foo() for functions and defined System V Release 2
  562. semantics to them so that there are no local variables
  563. and so that traps are not scoped.
  564. ksh93 keeps the ksh88
  565. semantics for functions defined as function name, and
  566. has changed the name() semantics to match the POSIX
  567. semantics.
  568. Clearly,
  569. <STRONG>function</STRONG>
  570. <EM>name</EM>
  571. is more useful.
  572. <DT>What is the naming conventions for files in FPATH and can one file contain more than one function definition?<DD>
  573. You can have more than one function defined in each file defined
  574. in FPATH and all of them will be added to the list of known
  575. functions. Any commands placed in this file outside of function
  576. definitions will be invoked first. The name of the file must be
  577. that of the first function you invoke. If you have several functions
  578. defined in one file, then you should create a link to each of the
  579. function names that can potentially be invoked first.
  580. <DT>What are name reference variables and how are they used?<DD>
  581. Reference variables are variables in which all references
  582. and assignments refer to the variable that they reference.
  583. For example,
  584. <DIV class=FI>
  585. <PRE>
  586. typeset -n name=$1
  587. name=value
  588. </DIV>
  589. </PRE>
  590. is equivalent to
  591. <DIV class=FI>
  592. <PRE>
  593. eval \$1='value'
  594. </DIV>
  595. </PRE>
  596. References are most useful for passing arguments such as
  597. arrays to functions.
  598. <DT>If i=1 and var1=some value, how do I print var$i to get its value?<DD>
  599. Either use
  600. <DIV class=FI>
  601. <PRE>
  602. eval print var\$i
  603. </DIV>
  604. </PRE>
  605. or
  606. <DIV class=FI>
  607. <PRE>
  608. typeset -n x=var$i
  609. print $x
  610. </DIV>
  611. </PRE>
  612. <DT>How can I shift the elements of an array?<DD>
  613. The shift special builtin-command only works for positional
  614. parameters.
  615. However, noting that array subscripts start at 0, you can use
  616. <DIV class=FI>
  617. <PRE>
  618. typeset -A name "${name&#0091;@&#0093;:1}"
  619. </DIV>
  620. </PRE>
  621. to shift the array.
  622. <DT>Why are the braces required with array references, e.g. ${x&#0091;1&#0093;}?<DD>
  623. It would be nice to do $x&#0091;1&#0093;, but the POSIX shell would expand $x
  624. and then search for the file pattern resulting by concatenating &#0091;1&#0093;.
  625. ksh is POSIX compatible.
  626. <DT>How do I get the list of subscript names for an associative array?<DD>
  627. The prefix operator ! in variable expansions can be used to get names.
  628. To get the names of subscripts for an array, associative
  629. or indexed, use ${!var&#0091;@&#0093;}.
  630. <DT>How do I do global substitutions on the contents of shell variables?<DD>
  631. Use // instead of / for global substitution, ${var//aa/bb} will
  632. expand to the value of with each "aa" replace by "bb".
  633. <DT>How can I convert %XX values to ascii?<DD>
  634. You can convert this to a sequence of ANSI C strings and then eval that
  635. string, for example suppose the variable 'foo' contains %XX strings, then
  636. <DIV class=FI>
  637. <PRE>
  638. eval print -r -- "\$'${foo//'%'@(??)/'\x\1"'\$'"}'"
  639. </DIV>
  640. </PRE>
  641. will print out the string in ascii.
  642. <DT>I want to use exec to open a file. How do I prevent the script from exiting if the exec fails?<DD>
  643. If you run
  644. <DIV class=FI>
  645. <PRE>
  646. command exec ... || error ...
  647. </DIV>
  648. </PRE>
  649. then error will be executed if the exec fails, but the script
  650. will not terminate.
  651. The command builtin will prevent the shell
  652. from exiting when special built-ins fail.
  653. <DT>How do I execute a builtin inside a function of the same name?<DD>
  654. You use the command builtin for this.
  655. For example,
  656. <DIV class=FI>
  657. <PRE>
  658. function cd
  659. {
  660. command cd "$@" &amp;&amp; title "$PWD"
  661. }
  662. </DIV>
  663. </PRE>
  664. will run the builtin command cd from within the function cd
  665. rather than calling the function cd recursively.
  666. <DT>How are variables scoped in ksh?<DD>
  667. The scoping of variables was not defined for ksh88 but in ksh93
  668. static scoping was specified.
  669. For example the output from
  670. <DIV class=FI>
  671. <PRE>
  672. function f1
  673. {
  674. print foo=$foo
  675. }
  676. function f2
  677. {
  678. typeset foo=local
  679. f1
  680. }
  681. foo=global
  682. f2
  683. </DIV>
  684. </PRE>
  685. will be "global".
  686. To get f2 to cause f1 to print the local
  687. value of foo, f2 can run "foo=$foo f1" instead.
  688. <DT>Can you write a self reproducing program in KornShell?<DD>
  689. Yes, the following program is self reproducing.
  690. Any shorter ones?
  691. <DIV class=FI>
  692. <PRE>
  693. n="
  694. " q="'" x="cat &lt;&lt;-!" y=! z='n="$n" q="$q" x="$x" y=$y z=$q$z$q$n$x$n$z$n$y'
  695. cat &lt;&lt;-!
  696. n="$n" q="$q" x="$x" y=$y z=$q$z$q$n$x$n$z$n$y
  697. !
  698. </DIV>
  699. </PRE>
  700. </DL>
  701. </DIV>
  702. <P>
  703. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="redirections">redirections</A></H3></FONT></FONT></CENTER>
  704. <DIV class=SH>
  705. <DL>
  706. <DT>How do I redirect both standard input and standard output to a file?<DD>
  707. Add the following redirections to the command.
  708. &gt; file 2&gt; &amp;1.
  709. This will redirect standard output (file descriptor 1) to "file" and
  710. standard error (file descriptor 2) to the same place as file descriptor 1.
  711. ksh redirection allows you to redirect any single digit file descriptor
  712. by putting the descriptor number in front of the redirection operator
  713. with no intervening space.
  714. <DT>Is there a way for the shell to pick the file number when I open a file?<DD>
  715. Yes, a redirection operator operator can be preceded by {n} without any
  716. intervening space where n is the name of a variable. The file descriptor
  717. will be placed in variable n.
  718. <DT>How do I connect to a socket from a shell script?<DD>
  719. exec 3&lt;&gt; /dev/tcp/hostname/portnum
  720. will open a tcp connection to portnum on hostname for
  721. reading and writing on file descriptor 3.
  722. You can then
  723. use read and print statements with file descriptor 3,
  724. or redirection operators &lt;&amp;3 or &gt;&amp;3 to use these connections.
  725. <DT>How do I seek to a given location in a file?<DD>
  726. The redirection operators &lt;# and &gt;# allow you to seek to a specified
  727. location in a file. The operator can be followed by an arithmetic
  728. expression contained in ((...)). The variables CUR and EOF can be used
  729. in the arithmetic expression to get relative locations or locations
  730. relative to the end of file respectively.
  731. Alternatively, &lt;# and &gt;# can be followed by a shell pattern. In this
  732. case, the file will be positioned to beginning of the next line
  733. containing this pattern.
  734. <DT>What is the the &lt;&lt;&lt; redirection operator?<DD>
  735. It denotes a here-document in which the document is contained the
  736. argument that follows &lt;&lt;&lt; and therefore there is no delimiter.
  737. <DT>What is the the &gt;; redirection operator?<DD>
  738. This operator writes the output into a temporary file in the same
  739. directory as the file specified after &gt;;. If the command completes
  740. successfully, then the the file is replaced. Otherwise, the
  741. original file is unchanged and the temporary file removed.
  742. <DT>What is the the &lt;&gt;; redirection operator?<DD>
  743. The file is opened for reading and writing as with &lt;&gt;. However,
  744. when the file is closed it is truncated to the its current location.
  745. </DL>
  746. </DIV>
  747. <P>
  748. <P><HR><CENTER><FONT color=red><FONT face=courier><H3><A name="extensions">extensions</A></H3></FONT></FONT></CENTER>
  749. <DIV class=SH>
  750. <DL>
  751. <DT>Is there a shell compiler?<DD>
  752. There is a separate command named shcomp that will convert
  753. a script into an intermediate machine independent form.
  754. The shell will detect this format whenever it runs a script and execute
  755. directly from this intermediate format.
  756. <DT>What is the advantage of making commands built-in?<DD>
  757. The startup time is reduced by a couple of orders of magnitude.
  758. In addition, built-in commands can access ksh internals.
  759. <DT>What is the disadvantage of making commands built-in?<DD>
  760. Errors in these built-ins can cause the shell to crash.
  761. <DT>How do I add built-in commands?<DD>
  762. There are two ways to do this.
  763. One is write a shared library
  764. with functions whose names are b_xxxx where xxxx is the name of
  765. the builtin.
  766. The function b_xxxx takes three arguments.
  767. The first
  768. two are the same as a mail program.
  769. The third parameter is
  770. a pointer argument which will point to the current shell context.
  771. The second way is to write a shared library with a function named
  772. lib_init().
  773. This function will be called with an argument of 0
  774. after the library is loaded.
  775. This function can add built-ins
  776. with the sh_addbuiltin() API function.
  777. In both cases, the
  778. library is loaded into the shell with the "builtin" utility.
  779. <DT>Can ksh93 be embedded?<DD>
  780. Yes, ksh93 can be compiled as a shared or dynamically linked
  781. library which can be embedded into applications.
  782. There is
  783. an API for interfacing to shell variables and to several of
  784. the internal shell functions.
  785. <DT>Can I write GUI applications with ksh?<DD>
  786. There are two extensions to ksh that can be used to write
  787. GUI applications as shell script.
  788. One is dtksh which
  789. was written by Steve Pendergrast at Novell and is
  790. included with the Common Desktop Environment, CDE.
  791. The other is
  792. tksh which was written by Jeff Korn.
  793. tksh combines the tk graphics
  794. package with ksh93 and reimplements the tcl language
  795. as an extension so that both tcl and ksh scripts
  796. can run in the same address space.
  797. The source for tksh
  798. is included in the ast-open package.
  799. </DL>
  800. </DIV>
  801. <TABLE cellpadding=4>
  802. <TR>
  803. <TD bgcolor=teal><A href='#' onclick='FAQ_mark("show")'><FONT color="#FFD87D">show all answers</FONT></A></TD>
  804. <TD bgcolor=teal><A href='#' onclick='FAQ_mark("hide")'><FONT color="#FFD87D">hide all answers</FONT></A></TD>
  805. </TR>
  806. </TABLE>
  807. <P>
  808. <HR>
  809. <TABLE border=0 align=center width=96%>
  810. <TR>
  811. <TD align=left></TD>
  812. <TD align=center></TD>
  813. <TD align=right>June 19, 2012</TD>
  814. </TR>
  815. </TABLE>
  816. <P>
  817. </TD></TR></TBODY></TABLE>
  818. </BODY>
  819. </HTML>