http://qs1969.pair.com?node_id=826954


in reply to Unable to grab STDERR from a backtick command

I think it depends on at what exact point the shell is doing the 2>&1 redirection.

I can't reproduce the problem with the command you've shown, i.e. here I do get

$ ./826946.pl CMD : >>echddo 'arghh'<< RETURN_CODE: >>32512<< CMD_RESULT : >>sh: echddo: not found <<

but when I remove one single quote ($cmd = "echddo arghh'"), so the shell command has a syntax error, I get like you

$ ./826946.pl sh: Syntax error: Unterminated quoted string CMD : >>echddo arghh'<< RETURN_CODE: >>512<< CMD_RESULT : >><<

The corresponding straces reveal that the redirection simply isn't being done in the latter case (note the missing dup2(1,2) ), presumably because my shell does the syntax checking before the 2>&1 redirection (which makes sense...), while in the other case, it does the redirection before starting to look for an executable 'echddo':

( cmd: echddo 'arghh' 2>&1 ) ... [pid 15950] rt_sigaction(SIGTERM, NULL, {SIG_DFL}, 8) = 0 [pid 15950] rt_sigaction(SIGTERM, {SIG_DFL}, NULL, 8) = 0 [pid 15950] fcntl(2, F_DUPFD, 10) = 10 [pid 15950] close(2) = 0 [pid 15950] fcntl(10, F_SETFD, FD_CLOEXEC) = 0 [pid 15950] dup2(1, 2) = 2 [pid 15950] stat("/home/almut/bin/echddo", 0x7fffb62454b0) = -1 ENOENT + (No such file or directory) [pid 15950] stat("/usr/local/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT + (No such file or directory) [pid 15950] stat("/usr/local/bin/echddo", 0x7fffb62454b0) = -1 ENOENT +(No such file or directory) [pid 15950] stat("/usr/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT (No s +uch file or directory) [pid 15950] stat("/usr/bin/echddo", 0x7fffb62454b0) = -1 ENOENT (No su +ch file or directory) [pid 15950] stat("/sbin/echddo", 0x7fffb62454b0) = -1 ENOENT (No such +file or directory) [pid 15950] stat("/bin/echddo", 0x7fffb62454b0) = -1 ENOENT (No such f +ile or directory) [pid 15950] write(2, "sh: ", 4) = 4 [pid 15950] write(2, "echddo: not found", 17) = 17 [pid 15950] write(2, "\n", 1) = 1 ... ( cmd: echddo arghh' 2>&1 ) ... [pid 15945] rt_sigaction(SIGTERM, NULL, {SIG_DFL}, 8) = 0 [pid 15945] rt_sigaction(SIGTERM, {SIG_DFL}, NULL, 8) = 0 [pid 15945] write(2, "sh: ", 4sh: ) = 4 [pid 15945] write(2, "Syntax error: Unterminated quote"..., 40Syntax e +rror: Unterminated quoted string) = 40 [pid 15945] write(2, "\n", 1 ...

What shell are you using?  (bash v3.2.39 here)

Your version seems to be trying to find the binary for 'echddo' before having set up the redirection...