dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libshell / misc / shell_styleguide.docbook
blobb0d438e3bf68f06a8b850dcfd5e016b0d96f69cf
1 <?xml version="1.0"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V5.0//EN" "http://www.oasis-open.org/docbook/xml/5.0b5/dtd/docbook.dtd" [
3 <!ENTITY tag_bourneonly '<inlinemediaobject><imageobject><imagedata fileref="images/tag_bourne.png"></imagedata></imageobject><textobject><phrase>[Bourne]</phrase></textobject></inlinemediaobject> '>
4 <!ENTITY tag_kshonly '<inlinemediaobject><imageobject><imagedata fileref="images/tag_ksh.png"></imagedata></imageobject><textobject><phrase>[ksh]</phrase></textobject></inlinemediaobject> '>
5 <!ENTITY tag_ksh88only '<inlinemediaobject><imageobject><imagedata fileref="images/tag_ksh88.png"></imagedata></imageobject><textobject><phrase>[ksh88]</phrase></textobject></inlinemediaobject> '>
6 <!ENTITY tag_ksh93only '<inlinemediaobject><imageobject><imagedata fileref="images/tag_ksh93.png"></imagedata></imageobject><textobject><phrase>[ksh93]</phrase></textobject></inlinemediaobject> '>
7 <!ENTITY tag_performance '<inlinemediaobject><imageobject><imagedata fileref="images/tag_perf.png"></imagedata></imageobject><textobject><phrase>[perf]</phrase></textobject></inlinemediaobject> '>
8 <!ENTITY tag_i18n '<inlinemediaobject><imageobject><imagedata fileref="images/tag_i18n.png"></imagedata></imageobject><textobject><phrase>[i18n]</phrase></textobject></inlinemediaobject> '>
9 <!ENTITY tag_l10n '<inlinemediaobject><imageobject><imagedata fileref="images/tag_l10n.png"></imagedata></imageobject><textobject><phrase>[l10n]</phrase></textobject></inlinemediaobject> '>
11 <!--
13 CDDL HEADER START
15 The contents of this file are subject to the terms of the
16 Common Development and Distribution License (the "License").
17 You may not use this file except in compliance with the License.
19 You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
20 or http://www.opensolaris.org/os/licensing.
21 See the License for the specific language governing permissions
22 and limitations under the License.
24 When distributing Covered Code, include this CDDL HEADER in each
25 file and include the License file at usr/src/OPENSOLARIS.LICENSE.
26 If applicable, add the following below this CDDL HEADER, with the
27 fields enclosed by brackets "[]" replaced with your own identifying
28 information: Portions Copyright [yyyy] [name of copyright owner]
30 CDDL HEADER END
32 -->
34 <!--
36 Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37 Use is subject to license terms.
39 -->
41 <!-- tag images were created like this:
42 $ (text="perf" ;
43 pbmtext -nomargins -lspace 0 -builtin fixed "${text}" |
44 pbmtopgm 1 1 |
45 pgmtoppm 1.0,1.0,1.0-0,0,0 /dev/stdin |
46 ppmtogif |
47 giftopnm |
48 pnmtopng >"tag_${text}.png")
49 -->
51 <!-- compile with:
52 xsltproc &minus;&minus;stringparam generate.section.toc.level 0 \
53 &minus;&minus;stringparam toc.max.depth 3 \
54 &minus;&minus;stringparam toc.section.depth 12 \
55 &minus;&minus;xinclude -o opensolaris_shell_styleguide.html /usr/share/sgml/docbook/docbook-xsl-stylesheets-1.69.1/html/docbook.xsl opensolaris_shell_styleguide.docbook
56 -->
58 <article
59 xmlns:xlink="http://www.w3.org/1999/xlink"
60 xmlns="http://docbook.org/ns/docbook"
61 xml:lang="en">
62 <!-- xmlns:xi="http://www.w3.org/2001/XInclude" -->
64 <info>
65 <title><emphasis>[DRAFT]</emphasis> Bourne/Korn Shell Coding Conventions</title>
67 <!-- subtitle abuse -->
68 <subtitle>
69 This page is currently work-in-progress until it is approved by the OS/Net community. Please send any comments to
70 <email>shell-discuss@opensolaris.org</email>.
71 </subtitle>
74 <authorgroup>
75 <!--
76 <author><personname>David G. Korn</personname><email>dgk@research.att.com</email></author>
77 <author><personname>Roland Mainz</personname><email>roland.mainz@nrubsig.org</email></author>
78 <author><personname>Mike Shapiro</personname><email>mike.shapiro@sun.com</email></author>
79 -->
80 <author><orgname>OpenSolaris.org</orgname></author>
81 </authorgroup>
82 </info>
84 <section xml:id="intro">
85 <title>Intro</title>
86 <para>This document describes the shell coding style used for all the SMF script changes integrated into (Open)Solaris.</para>
87 <para>All new SMF shell code should conform to this coding standard, which is intended to match our existing C coding standard.</para>
88 <para>When in doubt, think "what would be the C-Style equivalent ?" and "What does the POSIX (shell) standard say ?"</para>
89 </section><!-- end of intro -->
92 <section xml:id="rules">
93 <title>Rules</title>
97 <section xml:id="general">
98 <title>General</title>
100 <section xml:id="basic_format">
101 <title>Basic Format</title>
102 <para>Similar to <literal>cstyle</literal>, the basic format is that all
103 lines are indented by TABs or eight spaces, and continuation lines (which
104 in the shell end with "\") are indented by an equivalent number of TABs
105 and then an additional four spaces, e.g.
106 <programlisting>
107 cp foo bar
108 cp some_realllllllllllllllly_realllllllllllllly_long_path \
109 to_another_really_long_path
110 </programlisting>
111 </para>
112 <para>The encoding used for the shell scripts is either <literal>ASCII</literal>
113 or <literal>UTF-8</literal>, alternative encodings are only allowed when the
114 application requires this.</para>
115 </section>
118 <section xml:id="commenting">
119 <title>Commenting</title>
120 <para>Shell comments are preceded by the '<literal>#</literal>' character. Place
121 single-line comments in the right-hand margin. Use an extra '<literal>#</literal>'
122 above and below the comment in the case of multi-line comments:
123 <programlisting>
124 cp foo bar # Copy foo to bar
127 # Modify the permissions on bar. We need to set them to root/sys
128 # in order to match the package prototype.
130 chown root bar
131 chgrp sys bar
132 </programlisting>
133 </para>
134 </section>
137 <section xml:id="interpreter_magic">
138 <title>Interpreter magic</title>
139 <para>The proper interpreter magic for your shell script should be one of these:
140 <programlisting>
141 #!/bin/sh Standard Bourne shell script
142 #!/bin/ksh -p Standard Korn shell 88 script. You should always write ksh
143 scripts with -p so that ${ENV} (if set by the user) is not
144 sourced into your script by the shell.
145 #!/bin/ksh93 Standard Korn shell 93 script (-p is not needed since ${ENV} is
146 only used for interactive shell sessions).
147 </programlisting>
148 </para>
149 </section>
152 <section xml:id="harden_your_script_against_unexpected_input">
153 <title>Harden the script against unexpected (user) input</title>
154 <para>Harden your script against unexpected (user) input, including
155 command line options, filenames with blanks (or other special
156 characters) in the name, or file input</para>
157 </section>
160 <section xml:id="use_builtin_commands">
161 <title>&tag_kshonly;&tag_performance;Use builtin commands if the shell provides them</title>
162 <para>
163 Use builtin commands if the shell provides them. For example ksh93s+
164 (ksh93, version 's+') delivered with Solaris (as defined by PSARC 2006/550)
165 supports the following builtins:
166 <simplelist type="inline">
167 <member>basename</member>
168 <member>cat</member>
169 <member>chgrp</member>
170 <member>chmod</member>
171 <member>chown</member>
172 <member>cmp</member>
173 <member>comm</member>
174 <member>cp</member>
175 <member>cut</member>
176 <member>date</member>
177 <member>dirname</member>
178 <member>expr</member>
179 <member>fds</member>
180 <member>fmt</member>
181 <member>fold</member>
182 <member>getconf</member>
183 <member>head</member>
184 <member>id</member>
185 <member>join</member>
186 <member>ln</member>
187 <member>logname</member>
188 <member>mkdir</member>
189 <member>mkfifo</member>
190 <member>mv</member>
191 <member>paste</member>
192 <member>pathchk</member>
193 <member>rev</member>
194 <member>rm</member>
195 <member>rmdir</member>
196 <member>stty</member>
197 <member>tail</member>
198 <member>tee</member>
199 <member>tty</member>
200 <member>uname</member>
201 <member>uniq</member>
202 <member>wc</member>
203 <member>sync</member>
204 </simplelist>
205 Those builtins can be enabled via <literal>$ builtin name_of_builtin #</literal> in shell
206 scripts
207 </para>
208 </section>
211 <section xml:id="use_blocks_not_subshells">
212 <title>&tag_performance;Use blocks and not subshells if possible</title>
213 <para>Use blocks and not subshells if possible, e.g. use
214 <literal>$ { print "foo" ; print "bar" ; }</literal> instead of
215 <literal>$ (print "foo" ; print "bar") #</literal> - blocks are
216 faster since they do not require to save the subshell context (ksh93) or
217 trigger a shell child process (Bourne shell, bash, ksh88 etc.)
218 </para>
219 </section>
222 <section xml:id="use_long_options_for_set_builtin">
223 <title>&tag_kshonly; use long options for "<literal>set</literal>"</title>
224 <para>use long options for "<literal>set</literal>", for example instead of <literal>$ set -x #</literal>
225 use <literal>$ set -o xtrace #</literal> to make the code more readable.</para>
226 </section>
229 <section xml:id="use_posix_command_substitutions_syntax">
230 <title>&tag_kshonly; Use <literal>$(...)</literal> instead of <literal>`...`</literal> command substitutions</title>
231 <para>Use <literal>$(...)</literal> instead of <literal>`...`</literal> - <literal>`...`</literal>
232 is an obsolete construct in ksh+POSIX sh scripts and <literal>$(...)</literal>.is a cleaner design,
233 requires no escaping rules, allows easy nesting etc.</para>
235 <note><title>&tag_ksh93only; <literal>${ ...;}</literal>-style command substitutions</title>
236 <para>ksh93 has support for an alternative version of command substitutions with the
237 syntax <literal>${ ...;}</literal> which do not run in a subshell.
238 </para></note>
239 </section>
242 <section xml:id="put_command_substitution_result_in_quotes">
243 <title>&tag_kshonly; Always put the result of a <literal>$(...)</literal> or
244 <literal>$( ...;)</literal> command substitution in quotes</title>
245 <para>Always put the result of <literal>$( ... )</literal> or <literal>$( ...;)</literal> in
246 quotes (e.g. <literal>foo="$( ... )"</literal> or <literal>foo="$( ...;)"</literal>) unless
247 there is a very good reason for not doing it</para>
248 </section>
251 <section xml:id="always_set_path">
252 <title>Scripts should always set their <envar>PATH</envar></title>
253 <para>Scripts should always set their <envar>PATH</envar> to make sure they do not use
254 alternative commands by accident (unless the value of <envar>PATH</envar> is well-known
255 and guaranteed to be set by the caller)</para>
256 </section>
259 <section xml:id="make_sure_commands_are_available">
260 <title>Make sure that commands from other packages/applications are really installed on the machine</title>
261 <para>Scripts should make sure that commands in optional packages are really
262 there, e.g. add a "precheck" block in scipts to avoid later failure when
263 doing the main job</para>
264 </section>
267 <section xml:id="check_usage_of_boolean_variables">
268 <title>Check how boolean values are used/implemented in your application</title>
269 <para>Check how boolean values are used in your application.</para>
270 <para>For example:
271 <programlisting>
272 mybool=0
273 # do something
274 if [ $mybool -eq 1 ] ; then do_something_1 ; fi
275 </programlisting>
276 could be rewritten like this:
277 <programlisting>
278 mybool=false # (valid values are "true" or "false", pointing
279 # to the builtin equivalents of /bin/true or /bin/false)
280 # do something
281 if ${mybool} ; then do_something_1 ; fi
282 </programlisting>
284 <programlisting>
285 integer mybool=0 # values are 0 or 1
286 # do something
287 if (( mybool==1 )) ; then do_something_1 ; fi
288 </programlisting>
289 </para>
290 </section>
292 <section xml:id="shell_uses_characters_not_bytes">
293 <title>&tag_i18n;The shell always operates on <emphasis>characters</emphasis> not bytes</title>
294 <para>Shell scripts operate on characters and <emphasis>not</emphasis> bytes.
295 Some locales use multiple bytes (called "multibyte locales") to represent one character</para>
297 <note><para>ksh93 has support for binary variables which explicitly
298 operate on bytes, not characters. This is the <emphasis>only</emphasis> allowed
299 exception.</para></note>
300 </section>
303 <section xml:id="multibyte_locale_input">
304 <title>&tag_i18n;Multibyte locales and input</title>
305 <para>Think about whether your application has to handle file names or
306 variables in multibyte locales and make sure all commands used in your
307 script can handle such characters (e.g. lots of commands in Solaris's
308 <filename>/usr/bin/</filename> are <emphasis>not</emphasis> able to handle such values - either use ksh93
309 builtin constructs (which are guaranteed to be multibyte-aware)
310 /<para>
311 </section>
314 <section xml:id="use_external_filters_only_for_large_datasets">
315 <title>&tag_performance;Only use external filters like <literal>grep</literal>/<literal>sed</literal>/<literal>awk</literal>/etc.
316 if you want to process lots of data with them</title>
317 <para>Only use external filters like <literal>grep</literal>/<literal>sed</literal>/<literal>awk</literal>/etc.
318 if a significant amount of data is processed by the filter or if
319 benchmarking shows that the use of builtin commands is significantly slower
320 (otherwise the time and resources needed to start the filter are
321 far greater then the amount of data being processed,
322 creating a performance problem).</para>
323 <para>For example:
324 <programlisting>
325 if [ "$(echo "$x" | egrep '.*foo.*')" != "" ] ; then
326 do_something ;
327 done
328 </programlisting>
329 can be re-written using ksh93 builtin constructs, saving several
330 <literal>|fork()|+|exec()|</literal>'s:
331 <programlisting>
332 if [[ "${x}" == ~(E).*foo.* ]] ; then
333 do_something ;
334 done
335 </programlisting>
336 </para>
337 </section>
340 <section xml:id="use_dashdash_if_first_arg_is_variable">
341 <title>If the first operand of a command is a variable, use <literal>--</literal></title>
342 <para>If the first operand of a command is a variable, use <literal>--</literal>
343 for any command that accepts this as end of argument to
344 avoid problems if the variable expands to a value starting with <literal>-</literal>.
345 </para>
346 <note><para>
347 At least
348 <simplelist type="inline">
349 <member>print</member>
350 <member>/usr/bin/fgrep</member>
351 <member>/usr/bin/grep</member>
352 <member>/usr/bin/egrep</member>
353 </simplelist>
354 support <literal>--</literal> as "end of arguments"-terminator.
355 </para></note>
356 </section>
358 <section xml:id="use_export">
359 <title>&tag_kshonly;&tag_performance;Use <literal>$ export FOOBAR=val #</literal> instead of
360 <literal>$ FOOBAR=val ; export FOOBAR #</literal></title>
361 <para>Use <literal>$ export FOOBAR=val # instead of $ FOOBAR=val ; export FOOBAR #</literal> -
362 this is much faster.</para>
363 </section>
366 <section xml:id="use_subshell_around_set_dashdash_usage">
367 <title>Use a subshell (e.g. <literal>$ ( mycmd ) #</literal>) around places which use
368 <literal>set -- $(mycmd)</literal> and/or <literal>shift</literal></title>
369 <para>Use a subshell (e.g. <literal>$ ( mycmd ) #</literal>) around places which use
370 <literal>set -- $(mycmd)</literal> and/or <literal>shift</literal> unless the variable
371 affected is either a local one or if it's guaranteed that this variable will no longer be used
372 (be careful for loadable functions, e.g. ksh/ksh93's <literal>autoload</literal> !!!!)
373 </para>
374 </section>
377 <section xml:id="be_careful_with_tabs_in_script_code">
378 <title>Be careful with using TABS in script code, they are not portable
379 between editors or platforms</title>
380 <para>Be careful with using TABS in script code, they are not portable
381 between editors or platforms.</para>
382 <para>If you use ksh93 use <literal>$'\t'</literal> to include TABs in sources, not the TAB character itself.</para>
383 </section>
386 <section xml:id="centralise_error_exit">
387 <title>If you have multiple points where your application exits with an error
388 message create a central function for this purpose</title>
389 <para>If you have multiple points where your application exits with an error
390 message create a central function for this, e.g.
391 <programlisting>
392 if [ -z "$tmpdir" ] ; then
393 print -u2 "mktemp failed to produce output; aborting."
394 exit 1
396 if [ ! -d $tmpdir ] ; then
397 print -u2 "mktemp failed to create a directory; aborting."
398 exit 1
400 </programlisting>
401 should be replaced with
402 <programlisting>
403 function fatal_error
405 print -u2 "${progname}: $*"
406 exit 1
408 # do something (and save ARGV[0] to variable "progname")
409 if [ -z "$tmpdir" ] ; then
410 fatal_error "mktemp failed to produce output; aborting."
412 if [ ! -d "$tmpdir" ] ; then
413 fatal_error "mktemp failed to create a directory; aborting."
415 </programlisting>
416 </para>
417 </section>
420 <section xml:id="use_set_o_nounset">
421 <title>&tag_kshonly; Think about using <literal>$ set -o nounset #</literal> by default</title>
422 <para>Think about using <literal>$ set -o nounset #</literal> by default (or at least during the
423 script's development phase) to catch errors where variables are used
424 when they are not set (yet), e.g.
425 <screen>
426 $ <userinput>(set -o nounset ; print ${foonotset})</userinput>
427 <computeroutput>/bin/ksh93: foonotset: parameter not set</computeroutput>
428 </screen>
429 </para>
430 </section>
433 <section xml:id="avoid_eval_builtin">
434 <title>Avoid using <literal>eval</literal> unless absolutely necessary</title>
435 <para>Avoid using <literal>eval</literal> unless absolutely necessary. Subtle things
436 can happen when a string is passed back through the shell
437 parser. You can use name references to avoid uses such as
438 <literal>eval $name="$value"</literal>.
439 </para>
440 </section>
443 <section xml:id="use_concatenation_operator">
444 <title>&tag_ksh93only;Use the string/array concatenation operator <literal>+=</literal></title>
445 <para>Use <literal>+=</literal> instead of manually adding strings/array elements, e.g.
446 <programlisting>
447 foo=""
448 foo="${foo}a"
449 foo="${foo}b"
450 foo="${foo}c"
451 </programlisting>
452 should be replaced with
453 <programlisting>
454 foo=""
455 foo+="a"
456 foo+="b"
457 foo+="c"
458 </programlisting>
459 </para>
460 </section>
462 <section xml:id="use_source_not_dot">
463 <title>&tag_ksh93only;Use <literal>source</literal> instead of '<literal>.</literal> '(dot)
464 to include other shell script fragments</title>
465 <para>Use <literal>source</literal> instead of '<literal>.</literal>'
466 (dot) to include other shell script fragments - the new form is much
467 more readable than the tiny dot and a failure can be caught within the script.</para>
468 </section>
471 <section xml:id="use_builtin_localisation_support">
472 <title>&tag_ksh93only;&tag_performance;&tag_l10n;Use <literal>$"..."</literal> instead of
473 <literal>gettext ... "..."</literal> for strings that need to be localized for different locales</title>
474 <para>Use $"..." instead of <literal>gettext ... "..."</literal> for strings that need to be
475 localized for different locales. <literal>gettext</literal> will require a
476 <literal>fork()+exec()</literal> and
477 reads the whole catalog each time it's called, creating a huge overhead for localisation
478 (and the <literal>$"..."</literal> is easier to use, e.g. you only have to put a
479 <literal>$</literal> in front of the catalog and the string will be localised).
480 </para>
481 </section>
484 <section xml:id="use_set_o_noglob">
485 <title>&tag_kshonly;&tag_performance;Use <literal>set -o noglob</literal> if you do not need to expand files</title>
486 <para>If you don't expect to expand files, you can do set <literal>-f</literal>
487 (<literal>set -o noglob</literal>) as well. This way the need to use <literal>""</literal> is
488 greatly reduced.</para>
489 </section>
492 <section xml:id="use_empty_ifs_to_handle_spaces">
493 <title>&tag_ksh93only;Use <literal>IFS=</literal> to avoid problems with spaces in filenames</title>
494 <para>Unless you want to do word splitting, put <literal>IFS=</literal>
495 at the beginning of a command. This way spaces in
496 file names won't be a problem. You can do
497 <literal>IFS='delims' read -r</literal> line
498 to override <envar>IFS</envar> just for the <literal>read</literal> command. However,
499 you can't do this for the <literal>set</literal> builtin.</para>
500 </section>
503 <section xml:id="set_locale_when_comparing_against_localised_output">
504 <title>Set the message locale if you process output of tools which may be localised</title>
505 <para>Set the message locale (<envar>LC_MESSAGES</envar>) if you process output of tools which may be localised</para>
506 <example><title>Set <envar>LC_MESSAGES</envar> when testing for specific outout of the <filename>/usr/bin/file</filename> utility:</title>
507 <programlisting>
508 # set french as default message locale
509 export LC_MESSAGES=fr_FR.UTF-8
513 # test whether the file "/tmp" has the filetype "directory" or not
514 # we set LC_MESSAGES to "C" to ensure the returned message is in english
515 if [[ "$(LC_MESSAGES=C file /tmp)" = *directory ]] ; then
516 print "is a directory"
518 </programlisting>
519 <note><para>The environment variable <envar>LC_ALL</envar> always
520 overrides any other <envar>LC_*</envar> environment variables
521 (and <envar>LANG</envar>, too),
522 including <envar>LC_MESSAGES</envar>.
523 if there is the chance that <envar>LC_ALL</envar> may be set
524 replace <envar>LC_MESSAGES</envar> with <envar>LC_ALL</envar>
525 in the example above.</para></note>
526 </example>
527 </section>
529 <section xml:id="cleanup_after_yourself">
530 <title>Cleanup after yourself.</title>
531 <para>Cleanup after yourself. For example ksh/ksh93 have an <literal>EXIT</literal> trap which
532 is very useful for this.
533 </para>
534 <note><para>
535 Note that the <literal>EXIT</literal> trap is executed for a subshell and each subshell
536 level can run it's own <literal>EXIT</literal> trap, for example
537 <screen>
538 $ <userinput>(trap "print bam" EXIT ; (trap "print snap" EXIT ; print "foo"))</userinput>
539 <computeroutput>foo
540 snap
541 bam</computeroutput>
542 </screen>
543 </para></note>
544 </section>
546 <section xml:id="use_proper_exit_code">
547 <title>Use a proper <literal>exit</literal> code</title>
548 <para>Explicitly set the exit code of a script, otherwise the exit code
549 from the last command executed will be used which may trigger problems
550 if the value is unexpected.</para>
551 </section>
554 <section xml:id="shell_lint">
555 <title>&tag_ksh93only;Use <literal>shcomp -n scriptname.sh /dev/null</literal> to check for common errors</title>
556 <para>Use <literal>shcomp -n scriptname.sh /dev/null</literal> to
557 check for common problems (such as insecure, depreciated or ambiguous constructs) in shell scripts.</para>
558 </section>
559 </section><!-- end of general -->
565 <section xml:id="functions">
566 <title>Functions</title>
568 <section xml:id="use_functions">
569 <title>Use functions to break up your code</title>
570 <para>Use functions to break up your code into smaller, logical blocks.</para>
571 </section>
573 <section xml:id="do_not_reserved_keywords_for_function_names">
574 <title>Do not use function names which are reserved keywords in C/C++/JAVA or the POSIX shell standard</title>
575 <para>Do not use function names which are reserved keywords (or function names) in C/C++/JAVA or the POSIX shell standard
576 (to avoid confusion and/or future changes/updates to the shell language).
577 </para>
578 </section>
580 <section xml:id="use_ksh_style_function_syntax">
581 <title>&tag_kshonly;&tag_performance;Use ksh-style <literal>function</literal></title>
582 <para>It is <emphasis>highly</emphasis> recommended to use ksh style functions
583 (<literal>function foo { ... }</literal>) instead
584 of Bourne-style functions (<literal>foo() { ... }</literal>) if possible
585 (and local variables instead of spamming the global namespace).</para>
587 <warning><para>
588 The difference between old-style Bourne functions and ksh functions is one of the major differences
589 between ksh88 and ksh93 - ksh88 allowed variables to be local for Bourne-style functions while ksh93
590 conforms to the POSIX standard and will use a function-local scope for variables declared in
591 Bourne-style functions.</para>
592 <para>Example (note that "<literal>integer</literal>" is an alias for "<literal>typeset -li</literal>"):
593 <programlisting>
594 # new style function with local variable
595 $ ksh93 -c 'integer x=2 ; function foo { integer x=5 ; } ; print "x=$x"
596 ; foo ; print "x=$x" ;'
599 # old style function with an attempt to create a local variable
600 $ ksh93 -c 'integer x=2 ; foo() { integer x=5 ; } ; print "x=$x" ; foo ;
601 print "x=$x" ;'
604 </programlisting>
606 <uri xlink:href="http://www.opensolaris.org/os/project/ksh93-integration/docs/ksh93r/general/compatibility/">usr/src/lib/libshell/common/COMPATIBILITY</uri>
607 says about this issue:
608 <blockquote><para>
609 Functions, defined with name() with ksh-93 are compatible with
610 the POSIX standard, not with ksh-88. No local variables are
611 permitted, and there is no separate scope. Functions defined
612 with the function name syntax, maintain compatibility.
613 This also affects function traces.
614 </para></blockquote>
615 </para></warning>
617 </section>
620 <section xml:id="use_proper_return_code">
621 <title>Use a proper <literal>return</literal> code</title>
622 <para>Explicitly set the return code of a function - otherwise the exit code
623 from the last command executed will be used which may trigger problems
624 if the value is unexpected.</para>
625 <para>The only allowed exception is if a function uses the shell's <literal>errexit</literal> mode to leave
626 a function, subshell or the script if a command returns a non-zero exit code.
627 </para>
628 </section>
630 <section xml:id="use_fpath_to_load_common_code">
631 <title>&tag_kshonly;Use <envar>FPATH</envar> to load common functions, not <literal>source</literal></title>
632 <para>
633 Use the ksh <envar>FPATH</envar> (function path) feature to load functions which are shared between scripts
634 and not <literal>source</literal> - this allows to load such a function on demand and not all at once.</para>
635 </section>
637 </section><!-- end of functions -->
642 <section xml:id="if_for_while">
643 <title><literal>if</literal>, <literal>for</literal> and <literal>while</literal></title>
645 <section xml:id="if_for_while_format">
646 <title>Format</title>
647 <para>To match <literal>cstyle</literal>, the shell token equivalent to the <literal>C</literal>
648 "<literal>{</literal>" should appear on the same line, separated by a
649 "<literal>;</literal>", as in:
650 <programlisting>
651 if [ "$x" = "hello" ] ; then
652 echo $x
655 if [[ "$x" = "hello" ]] ; then
656 print $x
659 for i in 1 2 3; do
660 echo $i
661 done
663 for ((i=0 ; i &lt; 3 ; i++)); do
664 print $i
665 done
667 while [ $# -gt 0 ]; do
668 echo $1
669 shift
670 done
672 while (( $# &gt; 0 )); do
673 print $1
674 shift
675 done
676 </programlisting>
677 </para>
678 </section>
681 <section xml:id="test_builtin">
682 <title><literal>test</literal> Builtin</title>
683 <para>DO NOT use the test builtin. Sorry, executive decision.</para>
684 <para>In our Bourne shell, the <literal>test</literal> built-in is the same as the "["
685 builtin (if you don't believe me, try "type test" or refer to <filename>usr/src/cmd/sh/msg.c</filename>).</para>
686 <para>
687 So please do not write:
688 <programlisting>
689 if test $# -gt 0 ; then
690 </programlisting>
691 instead use:
692 <programlisting>
693 if [ $# -gt 0 ] ; then
694 </programlisting>
695 </para>
696 </section>
699 <section xml:id="use_ksh_test_syntax">
700 <title>&tag_kshonly;&tag_performance;Use "<literal>[[ expr ]]</literal>" instead of "<literal>[ expr ]</literal>"</title>
701 <para>Use "<literal>[[ expr ]]</literal>" instead of "<literal>[ expr ]</literal>" if possible
702 since it avoids going through the whole pattern expansion/etc. machinery and
703 adds additional operators not available in the Bourne shell, such as short-circuit
704 <literal>&amp;&amp;</literal> and <literal>||</literal>.
705 </para>
706 </section>
709 <section xml:id="use_posix_arithmetic_expressions">
710 <title>&tag_kshonly; Use "<literal>(( ... ))</literal>" for arithmetic expressions</title>
711 <para>Use "<literal>(( ... ))</literal>" instead of "<literal>[ expr ]</literal>"
712 or "<literal>[[ expr ]]</literal>" expressions.
713 </para>
714 <para>
715 Example: Replace
716 <programlisting>
718 # do something
719 if [ $i -gt 5 ] ; then
720 </programlisting>
721 with
722 <programlisting>
724 # do something
725 if (( i &gt; 5 )) ; then
726 </programlisting>
727 </para>
728 </section>
731 <section xml:id="compare_exit_code_using_math">
732 <title>&tag_kshonly;&tag_performance;Compare exit code using arithmetic expressions expressions</title>
733 <para>Use POSIX arithmetic expressions to test for exit/return codes of commands and functions.
734 For example turn
735 <programlisting>
736 if [ $? -gt 0 ] ; then
737 </programlisting>
738 into
739 <programlisting>
740 if (( $? &gt; 0 )) ; then
741 </programlisting>
742 </para>
743 </section>
746 <section xml:id="use_builtin_commands_in_loops">
747 <title>&tag_bourneonly; Use builtin commands in conditions for <literal>while</literal> endless loops</title>
748 <para>Make sure that your shell has a "<literal>true</literal>" builtin (like ksh93) when
749 executing endless loops like <literal>$ while true ; do do_something ; done #</literal> -
750 otherwise each loop cycle runs a <literal>|fork()|+|exec()|</literal>-cycle to run
751 <filename>/bin/true</filename>
752 </para>
753 </section>
756 <section xml:id="single_line_if_statements">
757 <title>Single-line if-statements</title>
758 <para>It is permissible to use <literal>&amp;&amp;</literal> and <literal>||</literal> to construct
759 shorthand for an "<literal>if</literal>" statement in the case where the if statement has a
760 single consequent line:
761 <programlisting>
762 [ $# -eq 0 ] &amp;&amp; exit 0
763 </programlisting>
764 instead of the longer:
765 <programlisting>
766 if [ $# -eq 0 ]; then
767 exit 0
769 </programlisting>
770 </para>
771 </section>
774 <section xml:id="exit_status_and_if_for_while">
775 <title>Exit Status and <literal>if</literal>/<literal>while</literal> statements</title>
776 <para>Recall that "<literal>if</literal>" and "<literal>while</literal>"
777 operate on the exit status of the statement
778 to be executed. In the shell, zero (0) means true and non-zero means false.
779 The exit status of the last command which was executed is available in the $?
780 variable. When using "<literal>if</literal>" and "<literal>while</literal>",
781 it is typically not necessary to use
782 <literal>$?</literal> explicitly, as in:
783 <programlisting>
784 grep foo /etc/passwd &gt;/dev/null 2>&amp;1
785 if [ $? -eq 0 ]; then
786 echo "found"
788 </programlisting>
789 Instead, you can more concisely write:
790 <programlisting>
791 if grep foo /etc/passwd &gt;/dev/null 2>&amp;1; then
792 echo "found"
794 </programlisting>
795 Or, when appropriate:
796 <programlisting>
797 grep foo /etc/passwd &gt;/dev/null 2>&amp;1 &amp;&amp; echo "found"
798 </programlisting>
799 </para>
800 </section>
802 </section><!-- end of if/for/while -->
809 <section xml:id="variables">
810 <title>Variable types, naming and usage</title>
812 <section xml:id="names_should_be_lowercase">
813 <title>Names of local, non-environment, non-constant variables should be lowercase</title>
814 <para>Names of variables local to the current script which are not exported to the environment
815 should be lowercase while variable names which are exported to the
816 environment should be uppercase.</para>
817 <para>The only exception are global constants (=global readonly variables,
818 e.g. <literal>$ float -r M_PI=3.14159265358979323846 #</literal> (taken from &lt;math.h&gt;))
819 which may be allowed to use uppercase names, too.
820 </para>
822 <warning><para>
823 Uppercase variable names should be avoided because there is a good chance
824 of naming collisions with either special variable names used by the shell
825 (e.g. <literal>PWD</literal>, <literal>SECONDS</literal> etc.).
826 </para></warning>
827 </section>
829 <section xml:id="do_not_reserved_keywords_for_variable_names">
830 <title>Do not use variable names which are reserved keywords/variable names in C/C++/JAVA or the POSIX shell standard</title>
831 <para>Do not use variable names which are reserved keywords in C/C++/JAVA or the POSIX shell standard
832 (to avoid confusion and/or future changes/updates to the shell language).
833 </para>
834 <note>
835 <para>The Korn Shell and the POSIX shell standard have many more
836 reserved variable names than the original Bourne shell. All
837 these reserved variable names are spelled uppercase.
838 </para>
839 </note>
840 </section>
842 <section xml:id="use_brackets_around_long_names">
843 <title>Always use <literal>'{'</literal>+<literal>'}'</literal> when using variable
844 names longer than one character</title>
845 <para>Always use <literal>'{'</literal>+<literal>'}'</literal> when using
846 variable names longer than one character unless a simple variable name is
847 followed by a blank, <literal>/</literal>, <literal>;</literal>, or <literal>$</literal>
848 character (to avoid problems with array,
849 compound variables or accidental misinterpretation by users/shell)
850 <programlisting>
851 print "$foo=info"
852 </programlisting>
853 should be rewritten to
854 <programlisting>
855 print "${foo}=info"
856 </programlisting>
857 </para>
858 </section>
861 <section xml:id="quote_variables_containing_filenames_or_userinput">
862 <title><emphasis>Always</emphasis> put variables into quotes when handling filenames or user input</title>
863 <para><emphasis>Always</emphasis> put variables into quotes when handling filenames or user input, even if
864 the values are hardcoded or the values appear to be fixed. Otherwise at
865 least two things may go wrong:
866 <itemizedlist>
867 <listitem><para>a malicious user may be able to exploit a script's inner working to
868 infect his/her own code</para></listitem>
869 <listitem><para>a script may (fatally) misbehave for unexpected input (e.g. file names
870 with blanks and/or special symbols which are interpreted by the shell)</para></listitem>
871 </itemizedlist>
872 </para>
874 <note><para>
875 As alternative a script may set <literal>IFS='' ; set -o noglob</literal> to turn off the
876 interpretation of any field seperators and the pattern globbing.
877 </para></note>
878 </section>
882 <section xml:id="use_typed_variables">
883 <title>&tag_kshonly;&tag_performance;Use typed variables if possible.</title>
884 <para>For example the following is very
885 inefficient since it transforms the integer values to strings and back
886 several times:
887 <programlisting>
891 # more code
892 if [ $a -lt 5 -o $b -gt c ] ; then do_something ; fi
893 </programlisting>
894 This could be rewritten using ksh constructs:
895 <programlisting>
896 integer a=0
897 integer b=1
898 integer c=2
899 # more code
900 if (( a &lt; 5 || b &gt; c )) ; then do_something ; fi
901 </programlisting>
902 </para>
903 </section>
906 <section xml:id="store_lists_in_arrays">
907 <title>&tag_ksh93only; Store lists in arrays or associative arrays</title>
908 <para>Store lists in arrays or associative arrays - this is usually easier
909 to manage.</para>
910 <para>
911 For example:
912 <programlisting>
914 /etc/foo
915 /etc/bar
916 /etc/baz
918 echo $x
919 </programlisting>
920 can be replaced with
921 <programlisting>
922 typeset -a mylist
923 mylist[0]="/etc/foo"
924 mylist[1]="/etc/bar"
925 mylist[2]="/etc/baz"
926 print "${mylist[@]}"
927 </programlisting>
928 or (ksh93-style append entries to a normal (non-associative) array)
929 <programlisting>
930 typeset -a mylist
931 mylist+=( "/etc/foo" )
932 mylist+=( "/etc/bar" )
933 mylist+=( "/etc/baz" )
934 print "${mylist[@]}"
935 </programlisting>
936 </para>
937 <note>
938 <title>Difference between expanding arrays with mylist[@] and mylist[*] subscript operators</title>
939 <para>
940 Arrays may be expanded using two similar subscript operators, @ and *. These subscripts
941 differ only when the variable expansion appears within double quotes. If the variable expansion
942 is between double-quotes, "${mylist[*]}" expands to a single string with the value of each array
943 member separated by the first character of the <envar>IFS</envar> variable, and "${mylist[@]}"
944 expands each element of name to a separate string.
945 </para>
946 <example><title>Difference between [@] and [*] when expanding arrays</title>
947 <programlisting>
948 typeset -a mylist
949 mylist+=( "/etc/foo" )
950 mylist+=( "/etc/bar" )
951 mylist+=( "/etc/baz" )
952 IFS=","
953 printf "mylist[*]={ 0=|%s| 1=|%s| 2=|%s| 3=|%s| }\n" "${mylist[*]}"
954 printf "mylist[@]={ 0=|%s| 1=|%s| 2=|%s| 3=|%s| }\n" "${mylist[@]}"
955 </programlisting>
956 <para>will print:</para>
957 <screen>
958 <computeroutput>mylist[*]={ 0=|/etc/foo,/etc/bar,/etc/baz| 1=|| 2=|| 3=|| }
959 mylist[@]={ 0=|/etc/foo| 1=|/etc/bar| 2=|/etc/baz| 3=|| }
960 </computeroutput>
961 </screen>
962 </example>
963 </note>
964 </section>
967 <section xml:id="use_compound_variables_or_lists_for_grouping">
968 <title>&tag_ksh93only; Use compound variables or associative arrays to group similar variables together</title>
969 <para>Use compound variables or associative arrays to group similar variables together.</para>
970 <para>
971 For example:
972 <programlisting>
973 box_width=56
974 box_height=10
975 box_depth=19
976 echo "${box_width} ${box_height} ${box_depth}"
977 </programlisting>
978 could be rewritten to ("associative array"-style)
979 <programlisting>
980 typeset -A -E box=( [width]=56 [height]=10 [depth]=19 )
981 print -- "${box[width]} ${box[height]} ${box[depth]}"
982 </programlisting>
983 or ("compound variable"-style
984 <programlisting>
985 box=(
986 float width=56
987 float height=10
988 float depth=19
990 print -- "${box.width} ${box.height} ${box.depth}"
991 </programlisting>
992 </para>
993 </section>
994 </section><!-- end of variables -->
1002 <section xml:id="io">
1003 <title>I/O</title>
1005 <section xml:id="avoid_echo">
1006 <title>Avoid using the "<literal>echo</literal>" command for output</title>
1007 <para>The behaviour of "<literal>echo</literal>" is not portable
1008 (e.g. System V, BSD, UCB and ksh93/bash shell builtin versions all
1009 slightly differ in functionality) and should be avoided if possible.
1010 POSIX defines the "<literal>printf</literal>" command as replacement
1011 which provides more flexible and portable behaviour.</para>
1013 <note>
1014 <title>&tag_kshonly;Use "<literal>print</literal>" and not "<literal>echo</literal>" in Korn Shell scripts</title>
1015 <para>Korn shell scripts should prefer the "<literal>print</literal>"
1016 builtin which was introduced as replacement for "<literal>echo</literal>".</para>
1017 <caution>
1018 <para>Use <literal>$ print -- ${varname}" #</literal> when there is the slightest chance that the
1019 variable "<literal>varname</literal>" may contain symbols like "-". Or better use "<literal>printf</literal>"
1020 instead, for example
1021 <programlisting>
1022 integer fx
1023 # do something
1024 print $fx
1025 </programlisting>
1026 may fail if "f" contains a negative value. A better way may be to use
1027 <programlisting>
1028 integer fx
1029 # do something
1030 printf "%d\n" fx
1031 </programlisting>
1032 </para>
1033 </caution>
1034 </note>
1035 </section>
1037 <section xml:id="use_redirect_not_exec_to_open_files">
1038 <title>&tag_ksh93only;Use <literal>redirect</literal> and not <literal>exec</literal> to open files</title>
1039 <para>Use <literal>redirect</literal> and not <literal>exec</literal> to open files - <literal>exec</literal>
1040 will terminate the current function or script if an error occurs while <literal>redirect</literal>
1041 just returns a non-zero exit code which can be caught.</para>
1042 <para>Example:
1043 <programlisting>
1044 if redirect 5&lt;/etc/profile ; then
1045 print "file open ok"
1046 head &lt;&amp;5
1047 else
1048 print "could not open file"
1050 </programlisting>
1051 </para>
1052 </section>
1054 <section xml:id="group_identical_redirections_together">
1055 <title>&tag_performance;Avoid redirections per command when the output goes into the same file,
1056 e.g. <literal>$ echo "foo" &gt;xxx ; echo "bar" &gt;&gt;xxx ; echo "baz" &gt;&gt;xxx #</literal></title>
1057 <para>Each of the redirections above trigger an
1058 <literal>|open()|,|write()|,|close()|</literal>-sequence. It is much
1059 more efficient (and faster) to group the rediction into a block,
1060 e.g. <literal>{ echo "foo" ; echo "bar" ; echo "baz" } &gt;xxx #</literal></para>
1061 </section>
1064 <section xml:id="avoid_using_temporary_files">
1065 <title>&tag_performance;Avoid the creation of temporary files and store the values in variables instead</title>
1066 <para>Avoid the creation of temporary files and store the values in variables instead if possible</para>
1067 <para>
1068 Example:
1069 <programlisting>
1070 ls -1 &gt;xxx
1071 for i in $(cat xxx) ; do
1072 do_something ;
1073 done
1074 </programlisting>
1075 can be replaced with
1076 <programlisting>
1077 x="$(ls -1)"
1078 for i in ${x} ; do
1079 do_something ;
1080 done
1081 </programlisting>
1082 </para>
1083 <note><para>ksh93 supports binary variables (e.g. <literal>typeset -b varname</literal>) which can hold any value.</para></note>
1084 </section>
1087 <section xml:id="create_subdirs_for_multiple_temporary_files">
1088 <title>If you create more than one temporary file create an unique subdir</title>
1089 <para>If you create more than one temporary file create an unique subdir for
1090 these files and make sure the dir is writable. Make sure you cleanup
1091 after yourself (unless you are debugging).
1092 </para>
1093 </section>
1096 <section xml:id="use_dynamic_file_descriptors">
1097 <title>&tag_ksh93only;Use {n}&lt;file instead of fixed file descriptor numbers</title>
1098 <para>When opening a file use {n}&lt;file, where <envar>n</envar> is an
1099 integer variable rather than specifying a fixed descriptor number.</para>
1100 <para>This is highly recommended in functions to avoid that fixed file
1101 descriptor numbers interfere with the calling script.</para>
1102 <example><title>Open a network connection and store the file descriptor number in a variable</title>
1103 <programlisting>
1104 function cat_http
1106 integer netfd
1110 # open TCP channel
1111 redirect {netfd}&lt;&gt;"/dev/tcp/${host}/${port}"
1113 # send HTTP request
1114 request="GET /${path} HTTP/1.1\n"
1115 request+="Host: ${host}\n"
1116 request+="User-Agent: demo code/ksh93 (2007-08-30; $(uname -s -r -p))\n"
1117 request+="Connection: close\n"
1118 print "${request}\n" &gt;&amp;${netfd}
1120 # collect response and send it to stdout
1121 cat &lt;&amp;${netfd}
1123 # close connection
1124 exec {netfd}&lt;&amp;-
1129 </programlisting>
1130 </example>
1131 </section>
1134 <section xml:id="use_inline_here_documents">
1135 <title>&tag_ksh93only;&tag_performance;Use inline here documents
1136 instead of <literal>echo "$x" | command</literal></title>
1137 <para>Use inline here documents, for example
1138 <programlisting>
1139 command &lt;&lt;&lt; $x
1140 </programlisting>
1141 rather than
1142 <programlisting>
1143 print -r -- "$x" | command
1144 </programlisting>
1145 </para>
1146 </section>
1149 <section xml:id="use_read_r">
1150 <title>&tag_ksh93only;Use the <literal>-r</literal> option of <literal>read</literal> to read a line</title>
1151 <para>Use the <literal>-r</literal> option of <literal>read</literal> to read a line.
1152 You never know when a line will end in <literal>\</literal> and without a
1153 <literal>-r</literal> multiple
1154 lines can be read.</para>
1155 </section>
1158 <section xml:id="print_compound_variables_using_print_C">
1159 <title>&tag_ksh93only;Print compound variables using <literal>print -C varname</literal> or <literal>print -v varname</literal></title>
1160 <para>Print compound variables using <literal>print -C varname</literal> or
1161 <literal>print -v varname</literal> to make sure that non-printable characters
1162 are correctly encoded.</para>
1163 <example><title>Print compound variable with non-printable characters</title>
1164 <programlisting>
1165 compound x=(
1167 b="hello"
1170 e="$(printf "1\v3")" <co xml:id="co.vertical_tab1" />
1173 print -v x
1174 </programlisting>
1175 <para>will print:</para>
1176 <screen>
1177 <computeroutput>(
1179 b=hello
1182 e=$'1\0133' <co xml:id="co.vertical_tab2" />
1184 )</computeroutput>
1185 </screen>
1186 <calloutlist>
1187 <callout arearefs="co.vertical_tab1 co.vertical_tab2">
1188 <para>vertical tab, <literal>\v</literal>, octal=<literal>\013</literal>.</para>
1189 </callout>
1190 </calloutlist>
1191 </example>
1192 </section>
1194 <section xml:id="command_name_before_redirections">
1195 <title>Put the command name and arguments before redirections</title>
1196 <para>Put the command name and arguments before redirections.
1197 You can legally do <literal>$ &gt; file date</literal> instead of <literal>date &gt; file</literal>
1198 but don't do it.</para>
1199 </section>
1201 <section xml:id="enable_gmacs_editor_mode_for_user_prompts">
1202 <title>&tag_ksh93only;Enable the <literal>gmacs</literal> editor
1203 mode when reading user input using the <literal>read</literal> builtin</title>
1204 <para>Enable the <literal>gmacs</literal>editor mode before reading user
1205 input using the <literal>read</literal> builtin to enable the use of
1206 cursor+backspace+delete keys in the edit line</para>
1207 <example><title>Prompt user for a string with gmacs editor mode enabled</title>
1208 <programlisting>
1209 set -o gmacs <co xml:id="co.enable_gmacs" />
1210 typeset inputstring="default value"
1212 read -v<co xml:id="co.read_v" /> inputstring<co xml:id="co.readvar" />?"Please enter a string: "<co xml:id="co.prompt" />
1214 printf "The user entered the following string: '%s'\n" "${inputstring}"
1217 </programlisting>
1218 <calloutlist>
1219 <callout arearefs="co.enable_gmacs">
1220 <para>Enable gmacs editor mode.</para>
1221 </callout>
1222 <callout arearefs="co.read_v">
1223 <para>The value of the variable is displayed and used as a default value.</para>
1224 </callout>
1225 <callout arearefs="co.readvar">
1226 <para>Variable used to store the result.</para>
1227 </callout>
1228 <callout arearefs="co.prompt">
1229 <para>Prompt string which is displayed in stderr.</para>
1230 </callout>
1231 </calloutlist>
1232 </example>
1233 </section>
1234 </section><!-- end of I/O -->
1241 <section xml:id="math">
1242 <title>Math</title>
1244 <section xml:id="use_builtin_arithmetic_expressions">
1245 <title>&tag_kshonly;&tag_performance;Use builtin arithmetic expressions instead of external applications</title>
1246 <para>Use builtin (POSIX shell) arithmetic expressions instead of
1247 <filename>expr</filename>,
1248 <filename>bc</filename>,
1249 <filename>dc</filename>,
1250 <filename>awk</filename>,
1251 <filename>nawk</filename> or
1252 <filename>perl</filename>.
1253 </para>
1254 <note>
1255 <para>ksh93 supports C99-like floating-point arithmetic including special values
1256 such as
1257 <simplelist type="inline">
1258 <member>+Inf</member>
1259 <member>-Inf</member>
1260 <member>+NaN</member>
1261 <member>-NaN</member>
1262 </simplelist>.
1263 </para>
1264 </note>
1265 </section>
1268 <section xml:id="use_floating_point_arithmetic_expressions">
1269 <title>&tag_ksh93only; Use floating-point arithmetic expressions if
1270 calculations may trigger a division by zero or other exceptions</title>
1271 <para>Use floating-point arithmetic expressions if calculations may
1272 trigger a division by zero or other exceptions - floating point arithmetic expressions in
1273 ksh93 support special values such as <literal>+Inf</literal>/<literal>-Inf</literal> and
1274 <literal>+NaN</literal>/<literal>-NaN</literal> which can greatly simplify testing for
1275 error conditions, e.g. instead of a <literal>trap</literal> or explicit
1276 <literal>if ... then... else</literal> checks for every sub-expression
1277 you can check the results for such special values.
1278 </para>
1279 <para>Example:
1280 <screen>
1281 $ <userinput>ksh93 -c 'integer i=0 j=5 ; print -- "x=$((j/i)) "'</userinput>
1282 <computeroutput>ksh93: line 1: j/i: divide by zero</computeroutput>
1283 $ <userinput>ksh93 -c 'float i=0 j=-5 ; print -- "x=$((j/i)) "'</userinput>
1284 <computeroutput>x=-Inf</computeroutput>
1285 </screen>
1286 </para>
1287 </section>
1290 <section xml:id="use_printf_a_for_passing_float_values">
1291 <title>&tag_ksh93only; Use <literal>printf "%a"</literal> when passing floating-point values</title>
1292 <para>Use <literal>printf "%a"</literal> when passing floating-point values between scripts or
1293 as output of a function to avoid rounding errors when converting between
1294 bases.</para>
1295 <para>
1296 Example:
1297 <programlisting>
1298 function xxx
1300 float val
1302 (( val=sin(5.) ))
1303 printf "%a\n" val
1305 float out
1306 (( out=$(xxx) ))
1308 print -- $out
1309 </programlisting>
1310 This will print:
1311 <programlisting>
1312 -0.9589242747
1313 -0x1.eaf81f5e09933226af13e5563bc6p-01
1314 </programlisting>
1315 </para>
1316 </section>
1319 <section xml:id="put_constants_into_readonly_variables">
1320 <title>&tag_kshonly;&tag_performance;Put constant values into readonly variables</title>
1321 <para>Put constant values into readonly variables</para>
1322 <para>For example:
1323 <programlisting>
1324 float -r M_PI=3.14159265358979323846
1325 </programlisting>
1327 <programlisting>
1328 float M_PI=3.14159265358979323846
1329 readonly M_PI
1330 </programlisting>
1331 </para>
1332 </section>
1335 <section xml:id="avoid_unnecessary_string_number_conversions">
1336 <title>&tag_kshonly;&tag_performance;Avoid string to number
1337 (and/or number to string) conversions in arithmetic expressions
1338 expressions</title>
1339 <para>Avoid string to number and/or number to string conversions in
1340 arithmetic expressions expressions to avoid performance degradation
1341 and rounding errors.</para>
1342 <example><title>(( x=$x*2 )) vs. (( x=x*2 ))</title>
1343 <programlisting>
1344 float x
1346 (( x=$x*2 ))
1347 </programlisting>
1348 <para>
1349 will convert the variable "x" (stored in the machine's native
1350 <literal>|long double|</literal> datatype) to a string value in base10 format,
1351 apply pattern expansion (globbing), then insert this string into the
1352 arithmetic expressions and parse the value which converts it into the internal |long double| datatype format again.
1353 This is both slow and generates rounding errors when converting the floating-point value between
1354 the internal base2 and the base10 representation of the string.
1355 </para>
1356 <para>
1357 The correct usage would be:
1358 </para>
1359 <programlisting>
1360 float x
1362 (( x=x*2 ))
1363 </programlisting>
1364 <para>
1365 e.g. omit the '$' because it's (at least) redundant within arithmetic expressions.
1366 </para>
1367 </example>
1370 <example><title>x=$(( y+5.5 )) vs. (( x=y+5.5 ))</title>
1371 <programlisting>
1372 float x
1373 float y=7.1
1375 x=$(( y+5.5 ))
1376 </programlisting>
1377 <para>
1378 will calculate the value of <literal>y+5.5</literal>, convert it to a
1379 base-10 string value amd assign the value to the floating-point variable
1380 <literal>x</literal> again which will convert the string value back to the
1381 internal |long double| datatype format again.
1382 </para>
1383 <para>
1384 The correct usage would be:
1385 </para>
1386 <programlisting>
1387 float x
1388 float y=7.1
1390 (( x=y+5.5 ))
1391 </programlisting>
1392 <para>
1393 i.e. this will save the string conversions and avoid any base2--&gt;base10--&gt;base2-conversions.
1394 </para>
1395 </example>
1396 </section>
1399 <section xml:id="set_lc_numeric_when_using_floating_point">
1400 <title>&tag_ksh93only;Set <envar>LC_NUMERIC</envar> when using floating-point constants</title>
1401 <para>Set <envar>LC_NUMERIC</envar> when using floating-point constants to avoid problems with radix-point
1402 representations which differ from the representation used in the script, for example the <literal>de_DE.*</literal> locale
1403 use ',' instead of '.' as default radix point symbol.</para>
1404 <para>For example:
1405 <programlisting>
1406 # Make sure all math stuff runs in the "C" locale to avoid problems with alternative
1407 # radix point representations (e.g. ',' instead of '.' in de_DE.*-locales). This
1408 # needs to be set _before_ any floating-point constants are defined in this script)
1409 if [[ "${LC_ALL}" != "" ]] ; then
1410 export \
1411 LC_MONETARY="${LC_ALL}" \
1412 LC_MESSAGES="${LC_ALL}" \
1413 LC_COLLATE="${LC_ALL}" \
1414 LC_CTYPE="${LC_ALL}"
1415 unset LC_ALL
1417 export LC_NUMERIC=C
1419 float -r M_PI=3.14159265358979323846
1420 </programlisting>
1421 </para>
1423 <note><para>The environment variable <envar>LC_ALL</envar> always overrides all other <envar>LC_*</envar> variables,
1424 including <envar>LC_NUMERIC</envar>. The script should always protect itself against custom <envar>LC_NUMERIC</envar> and
1425 <envar>LC_ALL</envar> values as shown in the example above.
1426 </para></note>
1427 </section>
1431 </section><!-- end of math -->
1438 <section xml:id="misc">
1439 <title>Misc</title>
1441 <section xml:id="debug_use_lineno_in_ps4">
1442 <title>Put <literal>[${LINENO}]</literal> in your <envar>PS4</envar></title>
1443 <para>Put <literal>[${LINENO}]</literal> in your <envar>PS4</envar> prompt so that you will get line
1444 numbers with you run with <literal>-x</literal>. If you are looking at performance
1445 issues put <literal>$SECONDS</literal> in the <envar>PS4</envar> prompt as well.</para>
1446 </section>
1448 </section><!-- end of misc -->
1453 </section><!-- end of RULES -->
1458 </article>