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

Re: [bug?] test adf -ge 0



On Tue, 31 Aug 2004, James William Pye wrote:

> Using 4.2.0, why does this return successful?
> 
> Both sh(freebsd) and bash echo an error message and return 2.
> 
> > flaw@void:~ % echo $ZSH_VERSION
> > 4.2.0
> > flaw@void:~ % test sdf -ge 0
> > flaw@void:~ % echo $?
> > 0

It's because of this:

zsh% sdf=-1
zsh% test sdf -ge 0
zsh% echo $?
1
zsh% sdf=1
zsh% test sdf -gt 0
zsh% echo $?
0

That is, zsh's builtin test is interpreting "sdf" as a variable name and 
using the corresponding value, just as (( sdf > 0 )) would do.  The value 
of an unset variable in math context is 0.

To forestall the inevitable question about what happens when the value of
the variable is not a number:

zsh% sdf=auq
zsh% auq=42
zsh% echo $(( sdf ))
42

If you wonder why _that_ works, consider this:

zsh% sdf='[#13]qua'
zsh% qua='6 * 9'
zsh% echo $(( sdf ))
13#42



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