Bash get exit status of command when 'set -e' is active? -
i have -e set in bash scripts, run command , return value.
without doing set +e; some-command; res=$?; set -e dance, how can that?
from bash manual:
the shell not exit if command fails [...] part of command executed in && or || list [...].
so, do:
#!/bin/bash set -eu foo() { # exit code 0, 1, or 2 return $(( random % 3 )) } ret=0 foo || ret=$? echo "foo() exited with: $ret" example runs:
$ ./foo.sh foo() exited with: 1 $ ./foo.sh foo() exited with: 0 $ ./foo.sh foo() exited with: 2 this canonical way of doing it.
Comments
Post a Comment