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

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -