On 09/08/2010 10:18 AM, Eric Blake wrote:
With modern shells, it should work just as well to
{ command; } 2>/dev/null
except of course, really old shells will fork a subshell in order to
execute redirected compound commands, so unless we have some indication
that this was fixed before some other feature we rely on was fixed ;-)
In this case, I'm not worried if an old shell forks. Rather, I'm trying
to optimize things so that bash can start with fewer forks, making the
startup experience slightly faster, especially for platforms like cygwin.
Unfortunately, bash 4.1 also forks for '{ unknown; } 2>/dev/null'. So
the only way to optimize the goal scenario of bash is to play with fds.
Dash, on the other hand, forks for '(unknown)' but not for '{ unknown;
}'. On the other hand, I know that there have been patches proposed to
bug-bash to avoid forking in more scenarios, so someday bash may be as
efficient as dash.
we might need a test for that.
That's for sure. And I still plan to do a sampling of various shells to
confirm whether { command; } 2>/dev/null is sufficient at avoiding
output, regardless of whether it forks on older shells, as it looks
cleaner than using exec to fiddle around with fds.
Testing with truss on Solaris /bin/sh - running '{ unknown; }
2>/dev/null' is indeed enough to silence stderr, but the fork() occurs
the same as with a subshell. What's really weird is the number of
stat() calls in the same truss results: both the parent and the forked
child did a PATH search for unknown - you'd think that either the parent
would defer the PATH search to just the child if it's going to fork, or
else the parent would be smart enough to not fork if the PATH search
failed. Also, $? was set to 1 instead of 127.
FreeBSD /bin/sh is noisy for 'unknown 2>/dev/null', but silent for '{
unknown; } 2>/dev/null'; and both (unknown) and { unknown;} were
sufficiently optimized to avoid the fork.
Irix /bin/sh was noisy for 'unknown 2>/dev/null', but silent for '{
unknown; } 2>/dev/null'. I'm not sure how to get the equivalent of
strace on that platform.
AIX /bin/sh was already quiet for 'unknown 2>/dev/null'.
But this research doesn't help much if '{ unknown; } 2>/dev/null'
doesn't shave a fork.
--
Eric Blake ebl...@redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org