I would like like to terminate a script on any failure including inside a pipe. In bash, you can set -eo pipefail, but this doesn't work in ksh.
Example:
# x
set -eo pipefail
false | true
echo "done: $?" # reached in ksh, unexpected
false
echo "done2" # not reached in either shell, as expected
bash x # prints nothing, as expected
ksh x # done: 1
ksh --version # ... 93u+ 2012-08-01
Why would ksh not exit in this case?
Edit: Adding another test
I compared this to other shells and am getting different results:
-bash-5.0$ zsh -c 'set -eo pipefail; false | true; exit 2' ; echo $?
1
-bash-5.0$ ksh -c 'set -eo pipefail; false | true; exit 2'; echo $?
2
-bash-5.0$ bash -c 'set -eo pipefail; false | true; exit 2' ; echo $?
1
Other than a bug in ksh, I do not understand what could cause this behavior. According to man ksh:
-e Unless contained in a ⎪⎪ or && command, or the command
following an if while or until command or in the
pipeline following !, if a command has a non-zero exit # Pipeline does not follow !
status, execute the ERR trap, if set, and exit. This # Pipeline has a non-zero exit status
mode is disabled while reading profiles.
pipefail
A pipeline will not complete until all
components of the pipeline have completed, and
the return value will be the value of the last
non-zero command to fail or zero if no command
has failed.
ACCEPTED]
It looks like a regression introduced by ksh93u+. ksh93u worked as expected in that regard (and like zsh, bash, mksh, yash and busybox sh).
I've just raised the issue at https://github.com/ksh93/ksh/issues/121
Now fixed in https://github.com/ksh93/ksh/commit/c049eec854d506ad6110eb50c769b10224c60075, included since v1.0.0-beta.1
... 2020.0.0has the same behavior. I will update the question with another test - stonewareslord