Take a look at these attempts:
$ case `true` in 0) echo success ;; *) echo fail ;; esac fail $ if `true` ; then > echo "success" > else > echo "fail" > fi success
Now, why is the case statement failing? You might wonder why I don't just use the
if statement and I shall explain. My command if complex and might return different return codes on which I want act on. I don't want to run the command multiple times and I can't do:
my_command res = $? case $? in ... esac
This is because I use
set -e in my script and therefore if
my_command returns failure the script aborts.
But I have a workaround...
set +e my_command res=$? set -e case $? in ... esac
But this is ugly, so returning to my initial question... why can I just use the
case my_command in ... esac version?