On Saturday 12 November 2011, Jim Meyering wrote:
> Stefano Lattarini wrote:
> > This might cause annoying slow-downs on systems where forks are more
> > expensive (i.e., MinGW and Cygwin). What about doing something like
> > this instead, so that the extra forks are avoided when the shell is
> > Bash (as is the usual case under MinGW and Cygwin, if I'm not
> > mistaken):
> >
> > if test -n "$BASH_VERSION"; then
> > export_with_values () { export "$@"; }
> > else
> > export_with_values () { ... your implementation here ... }
> > fi
> >
> > My testing shows that this idiom should work with at least bash 4.1,
> > 3.2, 3.0, 2.05 and 2.0.
>
> I like the idea but prefer to test for the precise feature being replaced.
> Then we don't even penalize those who use dash or zsh.
>
Or you could do this, to shave off the extra fork on MinGW and Cygwin:
> It is tricky, due to the brokenness of Solaris' /bin/sh, but as we
> learned via init.sh, you have to eval the test in a sub-shell:
>
> if (eval "export v=x") 2>/dev/null; then
> export_with_values () { export "$@"; }
> else
> export_with_values () { ... Bruno's implementation here ... }
> fi
>
Or you could do this, to shave off the extra fork on MinGW and Cygwin:
if test -n "$BASH_VERSION" || (eval "export v=x") 2>/dev/null; then
export_with_values () { export "$@"; }
else
export_with_values () { ... Bruno's implementation here ... }
BTW, autoconf does similar optimizations; for example, when looking for
a portable "echo-like" command, `configure' does:
if test -z "$BASH_VERSION$ZSH_VERSION" \
&& (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then
...
Regards,
Stefano