[sf-lug] bash strict mode (set -e)? - not so fast unless you know these quirks
Michael Paoli
michael.paoli at berkeley.edu
Fri Aug 15 00:29:07 PDT 2025
Read The Fine Manual (RTFM).
expr(1) is another potential gottcha, if one isn't well paying attention.
EXIT STATUS
The following exit values shall be returned:
0 The expression evaluated to neither null nor zero, and the output
specified in STDOUT was successfully written to standard output.
1 The expression evaluated to null or zero, and the output specified
in STDOUT was successfully written to standard output.
2 Invalid expression error.
>2 Another error occurred.
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/expr.html
So, return of 0, generally to indicate successful, no errors, and
often also no exceptions to be noted.
non-zero isn't just for errors. E.g. often used to indicate
exceptional conditions or the like.
E.g. grep(1) exits non-zero if no matches were found. This of course
can be highly useful,
e.g as one can test the exit value to make useful determinations,
and to determine where to go from there in the code.
A most highly typical example of that is the use of the test or [ command.
On Thu, Aug 14, 2025 at 11:33 PM Ron <ron at bclug.ca> wrote:
> Ken Shaffer wrote on 2025-08-14 13:15:
> > Well, from the bash man page:
> > ((expression))
> > The expression is evaluated according to the rules
> > described below under ARITHMETIC EVALUATION. If the value of the
> > expression is non-zero, the return status is 0; otherwise the return
> > status is 1.
> > It does what it says in this case.
More information about the sf-lug
mailing list