Could someone please explain why this works, specifically the fact that I am not using ‘$’ character before the names of the variables inside the if statement? I have searched the Bash Reference Manual, but could not find an explanation.
#!/usr/bin/env bash
one="$1"
two="$2"
three="$3"
four="$4"
if [[ one -le two ]] && [[ three -ge four ]]; then
echo "TRUE: $one <= $two && $three >= $four"
else
echo "FALSE: $one <= $two && $three >= $four"
fi
I have also tested it with a loop like this, which works perfectly
for x1 in {1..3}; do
for x2 in {1..3}; do
for x3 in {1..3}; do
for x4 in {1..3}; do ./test $x1 $x2 $x3 $x4;
done; done; done; done | sort
$x1 $x2 $x3 $x4has dollar signs, does it not?-leand-geinterpret their arguments in a numeric context; when you pass something that looks like a variable name in a numeric context, bash tries looking at whether the variable's contents are a number.[[ ... ]]. If you can use[[ ... ]]instead of[ ... ], you can also use(( ... ))instead.if (( one <= two && three >= four )); then.one, for example, then(( one -le two ))will silently behave the same as(( 0 -le two )), while(( $one < $two ))will behave like(( < $two ))and produce a syntax error immediately.((...))is specifically designed for arithmetic expressions; it uses "real" comparison operators like<=instead of cryptic legacy operators like-le, and is virtually guaranteed to be available if[[ ... ]]is available. (By which I mean, both[[ ... ]]and(( ...))are non-standard, butbashprovides both, and I'm not aware of any shell that provides[[...]]but not((...))).)