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

Re: abort if any error

On Fri, Aug 6, 2021 at 11:42 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> $ . wonderful_function; wonderful_function
> But, ooops, my last tweak to the function busted something so it wasn't
> sourced and what ran was the previous version.  Is there some way of
> forcing any script or command line or anything at all to stop
> unconditionally in case of an error on sourcing?

The "." command should have returned an error (nozero) status if it
couldn't load the file, so the problem is that you used ";" between
the two commands.  That effectively ignores the exit status of the
first command and runs the second one regardless.  If you had used
"&&" instead, you would have gotten what you wanted.  There's nothing
you can put INSIDE a script file that will cause the sourcing shell to
interrupt a ";"-separated sequence of commands when the script has a
syntax error or other parse-time failure.

If your two sample commands had appeared in another script, instead of
at an interactive prompt, "setopt errexit" in that outer script would
have caused it to stop when the first command failed, but that should
be used carefully because it will cause the script to exit when ANY
command outside of a conditional test context returns a nonzero

You DO NOT want to put "setopt errexit" in a script you will be
loading directly with the "." (or "source") commands, because that can
cause the whole interactive shell to exit.  Using errexit in a
function definition nested in a script file is OK, but as noted above
won't do any good if the function has a syntax error.

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