From 608c89c8c6547885d571780852c9613f3948737f Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 3 Aug 2010 15:40:16 -0600 Subject: [PATCH] docs: mention ksh bug with function syntax * doc/autoconf.texi (Shell Functions): Document ksh93 limitation. Signed-off-by: Eric Blake --- ChangeLog | 5 +++++ doc/autoconf.texi | 28 +++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2cbd6900..3d3ace6b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-08-03 Eric Blake + + docs: mention ksh bug with function syntax + * doc/autoconf.texi (Shell Functions): Document ksh93 limitation. + 2010-08-03 Ralf Wildenhues Fix typo in Autotest color test, for dash testsuite failure. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index df0e298d..e292062d 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -16202,6 +16202,20 @@ hence read-only. Do not use it. Nowadays, it is difficult to find a shell that does not support shell functions at all. However, some differences should be expected. +When declaring a shell function, you must include whitespace between the +@samp{)} after the function name and the start of the compound +expression, to avoid upsetting @command{ksh}. While it is possible to +use any compound command, most scripts use @samp{@{@dots{}@}}. + +@example +$ @kbd{/bin/sh -c 'a()@{ echo hi;@}; a'} +hi +$ @kbd{ksh -c 'a()@{ echo hi;@}; a'} +ksh: syntax error at line 1: `@}' unexpected +$ @kbd{ksh -c 'a() @{ echo hi;@}; a'} +hi +@end example + Inside a shell function, you should not rely on the error status of a subshell if the last command of that subshell was @code{exit} or @code{trap}, as this triggers bugs in zsh 4.x; while Autoconf tries to @@ -16213,10 +16227,10 @@ function. This has the effect that using a function as the first command in a @command{trap} handler can cause problems. @example -$ @kbd{bash -c 'foo()@{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?} +$ @kbd{bash -c 'foo() @{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?} 2 2 -$ @kbd{ash -c 'foo()@{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?} +$ @kbd{ash -c 'foo() @{ echo $?; @}; trap foo 0; (exit 2); exit 2'; echo $?} 0 2 @end example @@ -16231,8 +16245,8 @@ Not all shells treat shell functions as simple commands impacted by @samp{set -e}, for example with Solaris 10 @command{bin/sh}: @example -$ @kbd{bash -c 'f()@{ return 1; @}; set -e; f; echo oops} -$ @kbd{/bin/sh -c 'f()@{ return 1; @}; set -e; f; echo oops} +$ @kbd{bash -c 'f() @{ return 1; @}; set -e; f; echo oops} +$ @kbd{/bin/sh -c 'f() @{ return 1; @}; set -e; f; echo oops} oops @end example @@ -16259,13 +16273,13 @@ Meanwhile, not all shells follow the Posix rule that the assignment must affect the current environment in the same manner as special built-ins. @example -$ @kbd{/bin/sh -c 'func()@{ echo $a;@}; a=1 func; echo $a'} +$ @kbd{/bin/sh -c 'func() @{ echo $a;@}; a=1 func; echo $a'} @result{} @result{} -$ @kbd{ash -c 'func()@{ echo $a;@}; a=1 func; echo $a'} +$ @kbd{ash -c 'func() @{ echo $a;@}; a=1 func; echo $a'} @result{}1 @result{} -$ @kbd{bash -c 'set -o posix; func()@{ echo $a;@}; a=1 func; echo $a'} +$ @kbd{bash -c 'set -o posix; func() @{ echo $a;@}; a=1 func; echo $a'} @result{}1 @result{}1 @end example -- 2.47.2