Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Inconsistent behavior of ERR_EXIT with conditionals



On Tue, Nov 8, 2022, at 12:36 AM, Bart Schaefer wrote:
> So you can see that zsh agrees with bash on your "Failure" cases.  The
> output of dash (as /bin/sh on ubuntu) is the same as zsh with the
> exception of Failure-b, which can't be tested because there is no
> equivalent of "declare" (or "local").

Here is a set of tests incorporating most of Philippe's examples
(some slightly modified).  In the output, "no" and "yes" are intended
to indicate whether the shell exited due to "set -e" / ERR_EXIT.
The tl;dr is that zsh agrees with other shells, except on three
constructs from Philippe's original email.

The shells I tested are zsh 5.9, bash 5.1.16(1)-release, ksh AJM
93u+ 2012-08-01, dash 0.5.11.5, and yash 2.52.  For more fun, see
<https://www.in-ulm.de/~mascheck/various/set-e/>.

	% head -n 100 *.sh(n) driver.zsh
	==> 1.sh <==
	f() {
	    { false && true; }
	}
	f
	printf '    no'

	==> 2.sh <==
	f() {
	    false && true
	}
	f
	printf '    no'

	==> 3.sh <==
	f() {
	    if true; then
	        false && true
	    fi
	}
	f
	printf '    no'

	==> 4.sh <==
	f() {
	    case foo in
	        *) false && true ;;
	    esac
	}
	f
	printf '    no'

	==> 5.sh <==
	f() {
	    (false && true)
	}
	f
	printf '    no'

	==> 6.sh <==
	: $(false)
	printf '    no'

	==> 7.sh <==
	if [ "$KSH_VERSION" ]; then
	    f() { typeset v=$(false); }
	else
	    f() { local v=$(false); }
	fi
	f
	printf '    no'

	==> 8.sh <==
	if
	    false
	    printf '    no'
	    true
	then
	    true
	fi

	==> 9.sh <==
	{
	    false
	    printf '    no'
	    true
	} && true

	==> 10.sh <==
	false && true
	printf '    no'

	==> 11.sh <==
	! true
	printf '    no'

	==> driver.zsh <==
	set -- *.sh(n)

	printf '     '
	printf ' %5s' $@
	echo

	for sh in zsh bash ksh dash yash; do
	    printf %4s: $sh
	    for arg; do
	        $sh -e $arg || printf '   yes'
	    done
	    echo
	done
	% zsh driver.zsh
	       1.sh  2.sh  3.sh  4.sh  5.sh  6.sh  7.sh  8.sh  9.sh 10.sh 11.sh
	 zsh:    no   yes    no    no   yes    no    no    no    no    no    no
	bash:   yes   yes   yes   yes   yes    no    no    no    no    no    no
	 ksh:   yes   yes   yes   yes   yes    no    no    no    no    no    no
	dash:   yes   yes   yes   yes   yes    no    no    no    no    no    no
	yash:   yes   yes   yes   yes   yes    no    no    no    no    no    no


> Bash disagrees with dash and
> zsh on your "Success" cases, at least at that version.

Bash disables "set -e" in command substitutions and has done so
since at least 1.14 (the oldest version I can test).  Bash 4.4 added
an option that causes command substitutions to inherit "set -e",
but it remains disabled by default (except in POSIX mode).


-- 
vq




Messages sorted by: Reverse Date, Date, Thread, Author