bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

behavior of arithmetic evaluation operator inside extended test operator


From: Inian Vasanth
Subject: behavior of arithmetic evaluation operator inside extended test operator
Date: Tue, 19 May 2020 18:10:30 +0530

Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
-DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale'
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib
-g -O2 -Wno-parentheses -Wno-format-security
uname output: Linux DevVM 3.10.0-123.20.1.el7.x86_64 #1 SMP Thu Jan 29
18:05:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.4
Patch Level: 0
Release Status: release

Description:

The behavior of arithmetic context operator $((..)) inside [[..]] is not so
well defined. The GNU bash documentation says that $((..)) is not one of
the valid primaries supported inside [[..]]. The downside is the operator
without $ when used as ((..)) just behaves as double grouping, but $((..))
behaves as a valid arithmetic evaluation followed by non empty string
comparison `-n`

Steps

The first snippet produces incorrect results because of lexicographic
comparison of the two operands, while the second does an actual arithmetic
evaluation which is NOT DOCUMENTED but in the end does a string not empty
check with (-n 0) of the arithmetic evaluation result (not the exit code).

This behavior needs to be explained clearly or documented well.

bash -cx '[[ (( 100 < 3 )) ]] && echo ok'
+ bash -cx '[[ (( 100 < 3 )) ]] && echo ok'
+ [[ 100 < 3 ]]
+ echo ok

bash -cx '[[ $(( 100 < 3 )) ]] && echo ok'
+ bash -cx '[[ $(( 100 < 3 )) ]] && echo ok'
+ [[ -n 0 ]]
+ echo ok
ok

bash -cx '[[ $(( 100 < 300 )) ]] && echo ok'
+ bash -cx '[[ $(( 100 < 300 )) ]] && echo ok'
+ [[ -n 1 ]]
+ echo ok
ok




-- 
Regards,
INIAN VASANTH P


reply via email to

[Prev in Thread] Current Thread [Next in Thread]