EDIT. based on the comments to this answer (thanks to user unknown and glenn jackman), it seems that when using bc
for a true/false test, the required bash
test is simply:
(( $(echo "$X < $Y" |bc) ))
... see the test results and script below
wheras, the comparison to -ne 0
is needed for the old style bash [ ]
test.
bash
does not natively handle floating point numbers, but you can call a utility such as bc
From man bc
- An arbitrary precision calculator language
X=3.1
Y=4.1
# This test has two superfluous components.
# See EDIT (above) and TESTS below
if (($(echo "scale=9; $X < $Y" |bc)!=0)) ;then
echo "wassup"
fi
TEST results:
if [ "1" ] true
[ "1" ] true
if [ "0" ] true
[ "0" ] true
if [ 1 ] true
[ 1 ] true
if [ 0 ] true
[ 0 ] true
if (( "1" )) true
(( "1" )) true
if (( "0" )) false
(( "0" )) false
if (( 1 )) true
(( 1 )) true
if (( 0 )) false
(( 0 )) false
echo "1<1"|bc true
echo "1<0"|bc true
TEST script:
printf 'if [ "1" ] '; if [ "1" ]; then echo true; else echo false; fi
printf ' [ "1" ] '; [ "1" ] && echo true || echo false
printf 'if [ "0" ] '; if [ "0" ]; then echo true; else echo false; fi
printf ' [ "0" ] '; [ "0" ] && echo true || echo false
echo
printf 'if [ 1 ] '; if [ 1 ]; then echo true; else echo false; fi
printf ' [ 1 ] '; [ 1 ] && echo true || echo false
printf 'if [ 0 ] '; if [ 0 ]; then echo true; else echo false; fi
printf ' [ 0 ] '; [ 0 ] && echo true || echo false
echo
printf 'if (( "1" )) '; if (("1")); then echo true; else echo false; fi
printf ' (( "1" )) '; (("1")) && echo true || echo false
printf 'if (( "0" )) '; if (("0")); then echo true; else echo false; fi
printf ' (( "0" )) '; (("0")) && echo true || echo false
echo
printf 'if (( 1 )) '; if (( 1 )); then echo true; else echo false; fi
printf ' (( 1 )) '; (( 1 )) && echo true || echo false
printf 'if (( 0 )) '; if (( 0 )); then echo true; else echo false; fi
printf ' (( 0 )) '; (( 0 )) && echo true || echo false
echo
printf 'echo "1<1"|bc '; echo "1<1"|bc >/dev/null && echo true || echo false
printf 'echo "1<0"|bc '; echo "1<0"|bc >/dev/null && echo true || echo false