Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
return status of the 'let' builtin
- X-seq: zsh-workers 54285
- From: Jun T <takimoto-j@xxxxxxxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: return status of the 'let' builtin
- Date: Sun, 5 Apr 2026 10:22:48 +0900
- Archived-at: <https://zsh.org/workers/54285>
- List-id: <zsh-workers.zsh.org>
[1] In zshmisc(1), ARITHMETIC EVALUATION:
The return status is 0 if the arithmetic value of the expression
is non-zero, 1 if it is zero, and 2 if an error occurred.
and very similar description for the 'let' builtin in zshbuiltins(1).
But:
% (( 1 / 0 )); echo $?
zsh: division by zero
2 # OK
% let '1 / 0'; echo $?
zsh: division by zero
1 # should be 2
I think we should check errflag in bin_let() as in execarith()
(see tentative patch below).
[2] github PR #159 says:
zsh -c 'let "0.0 / 0.0"; echo $?'
should result in 1, not 0
What is the correct return status of 'let 0.0/0.0' or
'(( 0.0/0.0 ))'? Currently,
% let 'x=0/0.0'; echo ret=$? x=$x
ret=0 x=NaN
% let 'x=1/0.0'; echo ret=$? x=$x
ret=0 x=Inf
Should we return 2 here?
diff --git a/Src/builtin.c b/Src/builtin.c
index 7c095149d..26de1ab53 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -7501,8 +7501,11 @@ bin_let(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int func))
while (*argv)
val = matheval(*argv++);
- /* Errors in math evaluation in let are non-fatal. */
- errflag &= ~ERRFLAG_ERROR;
+ if (errflag) {
+ /* Errors in math evaluation in let are non-fatal. */
+ errflag &= ~ERRFLAG_ERROR;
+ return 2;
+ }
/* should test for fabs(val.u.d) < epsilon? */
return (val.type == MN_INTEGER) ? val.u.l == 0 : val.u.d == 0.0;
}
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index ba9c65e5b..f932f1b0f 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -494,7 +494,7 @@
( unsetopt unset
let noexist==0 )
-1:Arithmetic, NO_UNSET part 3
+2:Arithmetic, NO_UNSET part 3
?(eval):2: noexist: parameter not set
print $(( "6+2" / "1+3" ))
Messages sorted by:
Reverse Date,
Date,
Thread,
Author