share
Unix & Linuxset -eo pipefail differs on ksh and bash
[+5] [1] stonewareslord
[2020-04-17 16:27:25]
[ bash shell pipe ksh ]
[ https://unix.stackexchange.com/questions/580739/set-eo-pipefail-differs-on-ksh-and-bash ]

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.
(1) You may use a questionable version of ksh93. It works as expected with the ksh93 t+ from 2010 from OpenSolaris and BTW: this is the fastest ksh93 ever seen. - schily
OpenBSD ksh93 u+ prints the done message, fwiw. - Shawn
@Shawn which is not surprising, since it's the same version as the OP's ;-) - user313992
I just tried on linux as well. Version ... 2020.0.0 has the same behavior. I will update the question with another test - stonewareslord
[+3] [2020-08-30 16:22:05] Stéphane Chazelas [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


1