]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
doc: mention 'for' syntax issue on older shells
authorEric Blake <eblake@redhat.com>
Thu, 4 Jun 2015 20:00:29 +0000 (14:00 -0600)
committerEric Blake <eblake@redhat.com>
Thu, 4 Jun 2015 21:58:24 +0000 (15:58 -0600)
Based on a report by Michael Felt, via Paul Eggert on the
coreutils list.

* doc/autoconf.texi (Limitations of Builtins) <for>: Document
problem with 'for var in ;'.

Signed-off-by: Eric Blake <eblake@redhat.com>
doc/autoconf.texi

index 8c4302d335bdf53d0f085ae7b3a2781b7fdc2a96..9eac07baccd2b1e8e8a99e812c91119aac55f329 100644 (file)
@@ -17757,6 +17757,42 @@ But keep in mind that Zsh, even in Bourne shell emulation mode, performs
 word splitting on @samp{$@{1+"$@@"@}}; see @ref{Shell Substitutions},
 item @samp{$@@}, for more.
 
+Posix requires support for a @command{for} loop with no list after
+@code{in}.  However, Solaris @command{/bin/sh} treats that as a syntax
+error.  It is possible to work around this by providing any shell word
+that expands to nothing, or by ignoring an obvious sentinel.
+
+@example
+$ @kbd{/bin/sh -c 'for a in $empty; do echo hi; done'}
+$ @kbd{/bin/sh -c 'for a in ; do echo hi; done'}
+/bin/sh: syntax error at line 1: `;' unexpected
+@end example
+
+This syntax problem is most frequently encountered in code that goes
+through several layers of expansion, such as an m4 macro or makefile
+variable used as a list body, where the first layer of expansion (m4 or
+make) can end up expanding to nothing in the version handed to the
+shell.  In the makefile context, one common workaround is to use a shell
+variable rather than a make variable as the source of the list.
+
+@example
+$ @kbd{cat Makefile}
+list =
+bad:
+       @@for arg in $(list); do echo $$arg; done
+good:
+       @@list='$(list)'; for arg in $$list; do echo $$arg; done
+$ @kbd{make bad 2&>1 | head -n1}
+sh: syntax error at line 1: `;' unexpected
+$ @kbd{make bad list='a b'}
+a
+b
+$ @kbd{make good}
+$ @kbd{make good list='a b'}
+a
+b
+@end example
+
 In Solaris @command{/bin/sh}, when the list of arguments of a
 @command{for} loop starts with @emph{unquoted} tokens looking like
 variable assignments, the loop is not executed on those tokens: