Hello Ben, Jim, * Ben Pfaff wrote on Mon, Jan 03, 2011 at 08:54:19PM CET: > Jim Meyering <j...@meyering.net> writes: > > > Bruce Korb wrote: > >> Does that "git diff-index" really need to be run in a separate > >> shell invocation? > >>> dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty= > > > > Yes. As I recall, that was required in order to ensure that even when > > the shell fails to find a "git" program, the stderr output > > is redirected. > > What the Autoconf manual says about this is: > > On the other hand, some shells, such as Solaris or FreeBSD > `/bin/sh', warn about missing programs before performing redirections. > Therefore, to silently check whether a program exists, it is necessary > to perform redirections on a subshell: > > $ /bin/sh -c 'nosuch 2>/dev/null' > nosuch: not found > $ /bin/sh -c '(nosuch) 2>/dev/null' > $ bash -c 'nosuch 2>/dev/null' > > I always understood the use of "sh -c" in these examples as a > device to show which shell was being invoked, not to advise > actually using "sh -c". Rather, I thought that the advice was to > put the invocation in a subshell, as "(nosuch) 2>/dev/null".
You are both right, on all accounts. Whether you use a sh -c instance and redirect stderr outside the argument to -c, or use a subshell and redirect stderr outside the subshell, both avoids the escaping of errors. Cheers, Ralf