There are recommendations for using the following options to make Bash unsuccessful:
set -o errexit
set -o nounset
set -o pipefail
However, these options do not work as expected for the Bash functions passed through ||.
eg. in script
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
my_function() {
test -z "$1"
echo "this SHOULD NOT be printed"
}
my_function 1 || echo "???"
my_function 1
echo "this will not be printed"
The line # 2will end the script with code 1 without output. This is what I expect.
The line # 1confuses me: it my_functionwill be completed successfully, printing "it SHOULD NOT be printed" and returning code 0, thus, "???" will not be printed.
How can I make Bash to process my_functionin line # 1in the same fast way as on line # 2?
source
share