I am trying to capture the return code of a system call. To make it simple, consider a command that you know returns code 1, for example the following bash script t.sh:
#! /bin/bash
exit 1
Then consider:
use warnings;
use strict;
use feature qw(say);
my $res=system("t.sh");
say ($?>>8);
$res=system("t.sh &>/dev/null");
say ($?>>8);
which prints
1
0
Why does the second system call (the one with error redirection) give me a zero return code?
&>?$res = system("t.sh > /dev/null 2>1")&>is not working though, since it is given in the manual)&>is a relatively new Bash feature (introduced in v4.x). Check your version withbash --version;&>works properly on my system with v4.1.2.&>is semantically equivalent to>word 2>&1, so you could dosystem('t.sh > /dev/null 2>&1');. Note the&in2>&1; if you leave this off, you will create a file named1in the current directory.