In Bash, can I redirect output and retain the return code?
Here's the concrete task I'm trying to accomplish. zsh behaves the way I like
$ zsh
$ which clang > /dev/null 2&>1 && echo clang || echo gcc
clang
$ which doesntexist > /dev/null 2&>1 && echo doesntexist || echo gcc
gcc
But, in bash:
$ bash
$ which clang > /dev/null 2&>1 && echo clang || echo gcc
gcc
Here's the general case:
$ which clang > /dev/null 2&>1; echo $?
1
$ which clang; echo $?
/usr/bin/clang
0
How can I make bash retain the return code when I redirect output?
No comments:
Post a Comment