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> '
>
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]
36 Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37 Use is subject to license terms.
41 <!-- tag images were created like this:
43 pbmtext -nomargins -lspace 0 -builtin fixed "${text}" |
45 pgmtoppm 1.0,1.0,1.0-0,0,0 /dev/stdin |
48 pnmtopng >"tag_${text}.png")
52 xsltproc −−stringparam generate.section.toc.level 0 \
53 −−stringparam toc.max.depth 3 \
54 −−stringparam toc.section.depth 12 \
55 −−xinclude -o opensolaris_shell_styleguide.html /usr/share/sgml/docbook/docbook-xsl-stylesheets-1.69.1/html/docbook.xsl opensolaris_shell_styleguide.docbook
59 xmlns:
xlink=
"http://www.w3.org/1999/xlink"
60 xmlns=
"http://docbook.org/ns/docbook"
62 <!-- xmlns:xi="http://www.w3.org/2001/XInclude" -->
65 <title><emphasis>[DRAFT]
</emphasis> Bourne/Korn Shell Coding Conventions
</title>
67 <!-- subtitle abuse -->
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>.
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>
80 <author><orgname>OpenSolaris.org
</orgname></author>
84 <section xml:
id=
"intro">
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">
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.
108 cp some_realllllllllllllllly_realllllllllllllly_long_path \
109 to_another_really_long_path
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>
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:
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.
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:
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).
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>
160 <section xml:
id=
"use_builtin_commands">
161 <title>&tag_kshonly;&tag_performance;Use builtin commands if the shell provides them
</title>
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>
169 <member>chgrp
</member>
170 <member>chmod
</member>
171 <member>chown
</member>
173 <member>comm
</member>
176 <member>date
</member>
177 <member>dirname
</member>
178 <member>expr
</member>
181 <member>fold
</member>
182 <member>getconf
</member>
183 <member>head
</member>
185 <member>join
</member>
187 <member>logname
</member>
188 <member>mkdir
</member>
189 <member>mkfifo
</member>
191 <member>paste
</member>
192 <member>pathchk
</member>
195 <member>rmdir
</member>
196 <member>stty
</member>
197 <member>tail
</member>
200 <member>uname
</member>
201 <member>uniq
</member>
203 <member>sync
</member>
205 Those builtins can be enabled via
<literal>$ builtin name_of_builtin #
</literal> in shell
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.)
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>
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.
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>
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>
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>
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>
274 if [ $mybool -eq
1 ] ; then do_something_1 ; fi
276 could be rewritten like this:
278 mybool=false # (valid values are
"true" or
"false", pointing
279 # to the builtin equivalents of /bin/true or /bin/false)
281 if ${mybool} ; then do_something_1 ; fi
285 integer mybool=
0 # values are
0 or
1
287 if (( mybool==
1 )) ; then do_something_1 ; fi
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>
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)
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>
325 if [
"$(echo "$x
" | egrep '.*foo.*')" !=
"" ] ; then
329 can be re-written using ksh93 builtin constructs, saving several
330 <literal>|fork()|+|exec()|
</literal>'s:
332 if [[
"${x}" == ~(E).*foo.* ]] ; then
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>.
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>
354 support
<literal>--
</literal> as
"end of arguments"-terminator.
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>
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> !!!!)
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>
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.
392 if [ -z
"$tmpdir" ] ; then
393 print -u2
"mktemp failed to produce output; aborting."
396 if [ ! -d $tmpdir ] ; then
397 print -u2
"mktemp failed to create a directory; aborting."
401 should be replaced with
405 print -u2
"${progname}: $*"
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."
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.
426 $
<userinput>(set -o nounset ; print ${foonotset})
</userinput>
427 <computeroutput>/bin/ksh93: foonotset: parameter not set
</computeroutput>
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>.
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.
452 should be replaced with
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>
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).
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>
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>
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>
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"
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>
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.
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
538 $
<userinput>(trap
"print bam" EXIT ; (trap
"print snap" EXIT ; print
"foo"))
</userinput>
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>
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>
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>
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).
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>
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>"):
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 ;
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:
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.
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.
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>
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>
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:
651 if [
"$x" =
"hello" ] ; then
655 if [[
"$x" =
"hello" ]] ; then
663 for ((i=
0 ; i
< 3 ; i++)); do
667 while [ $# -gt
0 ]; do
672 while (( $#
> 0 )); do
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>
687 So please do not write:
689 if test $# -gt
0 ; then
693 if [ $# -gt
0 ] ; then
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>&&</literal> and
<literal>||
</literal>.
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.
719 if [ $i -gt
5 ] ; then
725 if (( i
> 5 )) ; then
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.
736 if [ $? -gt
0 ] ; then
740 if (( $?
> 0 )) ; then
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>
756 <section xml:
id=
"single_line_if_statements">
757 <title>Single-line if-statements
</title>
758 <para>It is permissible to use
<literal>&&</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:
762 [ $# -eq
0 ]
&& exit
0
764 instead of the longer:
766 if [ $# -eq
0 ]; then
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:
784 grep foo /etc/passwd
>/dev/null
2>&1
785 if [ $? -eq
0 ]; then
789 Instead, you can more concisely write:
791 if grep foo /etc/passwd
>/dev/null
2>&1; then
795 Or, when appropriate:
797 grep foo /etc/passwd
>/dev/null
2>&1 && echo
"found"
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
<math.h
>))
819 which may be allowed to use uppercase names, too.
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.).
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).
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.
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)
853 should be rewritten to
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:
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>
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.
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
892 if [ $a -lt
5 -o $b -gt c ] ; then do_something ; fi
894 This could be rewritten using ksh constructs:
900 if (( a
< 5 || b
> c )) ; then do_something ; fi
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
928 or (ksh93-style append entries to a normal (non-associative) array)
931 mylist+=(
"/etc/foo" )
932 mylist+=(
"/etc/bar" )
933 mylist+=(
"/etc/baz" )
938 <title>Difference between expanding arrays with mylist[@] and mylist[*] subscript operators
</title>
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.
946 <example><title>Difference between [@] and [*] when expanding arrays
</title>
949 mylist+=(
"/etc/foo" )
950 mylist+=(
"/etc/bar" )
951 mylist+=(
"/etc/baz" )
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[@]}"
956 <para>will print:
</para>
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=|| }
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>
976 echo
"${box_width} ${box_height} ${box_depth}"
978 could be rewritten to (
"associative array"-style)
980 typeset -A -E box=( [width]=
56 [height]=
10 [depth]=
19 )
981 print --
"${box[width]} ${box[height]} ${box[depth]}"
983 or (
"compound variable"-style
990 print --
"${box.width} ${box.height} ${box.depth}"
994 </section><!-- end of variables -->
1002 <section xml:
id=
"io">
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>
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>
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
1026 may fail if "f
" contains a negative value. A better way may be to use
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>
1044 if redirect 5</etc/profile ; then
1045 print "file open ok
"
1048 print "could not open file
"
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
" >xxx ; echo "bar
" >>xxx ; echo "baz
" >>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
" } >xxx #</literal></para>
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>
1071 for i in $(cat xxx) ; do
1075 can be replaced with
1083 <note><para>ksh93 supports binary variables (e.g. <literal>typeset -b varname</literal>) which can hold any value.</para></note>
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).
1096 <section xml:id="use_dynamic_file_descriptors
">
1097 <title>&tag_ksh93only;Use {n}<file instead of fixed file descriptor numbers</title>
1098 <para>When opening a file use {n}<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>
1111 redirect {netfd}<>"/dev/tcp/${host}/${port}
"
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
" >&${netfd}
1120 # collect response and send it to stdout
1121 cat <&${netfd}
1124 exec {netfd}<&-
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
1139 command <<< $x
1143 print -r -- "$x
" | command
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>
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>
1170 e="$(printf
"1\v3")
" <co xml:id="co.vertical_tab1
" />
1175 <para>will print:</para>
1182 e=$'1\0133' <co xml:id="co.vertical_tab2
" />
1187 <callout arearefs="co.vertical_tab1 co.vertical_tab2
">
1188 <para>vertical tab, <literal>\v</literal>, octal=<literal>\013</literal>.</para>
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>$ > file date</literal> instead of <literal>date > file</literal>
1198 but don't do it.</para>
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>
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}
"
1219 <callout arearefs="co.enable_gmacs
">
1220 <para>Enable gmacs editor mode.</para>
1222 <callout arearefs="co.read_v
">
1223 <para>The value of the variable is displayed and used as a default value.</para>
1225 <callout arearefs="co.readvar
">
1226 <para>Variable used to store the result.</para>
1228 <callout arearefs="co.prompt
">
1229 <para>Prompt string which is displayed in stderr.</para>
1234 </section><!-- end of I/O -->
1241 <section xml:id="math
">
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>.
1255 <para>ksh93 supports C99-like floating-point arithmetic including special values
1257 <simplelist type="inline
">
1258 <member>+Inf</member>
1259 <member>-Inf</member>
1260 <member>+NaN</member>
1261 <member>-NaN</member>
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.
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>
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
1313 -0x1.eaf81f5e09933226af13e5563bc6p-01
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>
1324 float -r M_PI=3.14159265358979323846
1328 float M_PI=3.14159265358979323846
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
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>
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.
1357 The correct usage would be:
1365 e.g. omit the '$' because it's (at least) redundant within arithmetic expressions.
1370 <example><title>x=$(( y+5.5 )) vs. (( x=y+5.5 ))</title>
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.
1384 The correct usage would be:
1393 i.e. this will save the string conversions and avoid any base2-->base10-->base2-conversions.
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>
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
1411 LC_MONETARY="${LC_ALL}
" \
1412 LC_MESSAGES="${LC_ALL}
" \
1413 LC_COLLATE="${LC_ALL}
" \
1414 LC_CTYPE="${LC_ALL}
"
1419 float -r M_PI=3.14159265358979323846
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.
1431 </section><!-- end of math -->
1438 <section xml:id="misc
">
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>
1448 </section><!-- end of misc -->
1453 </section><!-- end of RULES -->