On 08/24/2011 10:07 AM, Sam Steingold wrote:
* Eric Blake<roy...@erqung.pbz> [2011-08-24 09:31:45 -0600]:
f(){ echo a=$a b=$b c=$c ; }
f
a= b= c=
a=a b=b f
a=a b=b c=
f
a=a b=b c=
Which is indeed correct under the rules for POSIX
This sucks big time.
Such is life when dealing with shell portability.
So if I want to bind a variable for an eval invocation and do this:
eval "`./libtool --tag=CC --config | grep '^archive_cmds='`"
CC='${CC}' libobjs='$libs' deplibs='${CLFLAGS}' compiler_flags='${CFLAGS}' \
soname='$dll' lib='$lib' output_objdir='$dyndir' \
eval XCC_CREATESHARED=\"${archive_cmds}\"
and I want CC to have an old value after the second eval, I need to save
it and restore it by hand, like this:
CC_save=$CC
CC='${CC}' libobjs='$libs' deplibs='${CLFLAGS}' compiler_flags='${CFLAGS}' \
soname='$dll' lib='$lib' output_objdir='$dyndir' \
eval XCC_CREATESHARED=\"${archive_cmds}\"
CC=$CC_save
however, this does not distinguish between unset CC and CC=''.
(is there a way to distinguish these two situations?)
Yes - autoconf does this all the time, using an idiom roughly like this:
CC_set=${CC+set}
CC_save=$CC
do stuff that modifies $CC
if test "$CC_set" = set; then
CC=$CC_save
else
unset CC
fi
Also, you can use command to suppress the ability of built-ins like eval
(but not function calls) to affect the current environment:
$ unset foo
$ foo=bar eval :
$ echo $foo
bar
$ unset foo
$ foo=bar command eval :
$ echo $foo
$
--
Eric Blake ebl...@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org