How should I change this to check whether val
has an even or odd numeric value?
val=2
if $((RANDOM % $val)); ...
How should I change this to check whether val
has an even or odd numeric value?
val=2
if $((RANDOM % $val)); ...
$ a=4
$ [ $((a%2)) -eq 0 ] && echo "even"
even
$ a=3
$ [ $((a%2)) -eq 0 ] && echo "even"
$(( ... ))
is just an expression. Its result appears where bash
expects a command.
A POSIX-compatible solution would be:
if [ "$(( RANDOM % 2))" -ne 0 ];
but since RANDOM
isn't defined in POSIX either, you may as well use the right bash
command for the job: an arithmetic evaluation compound command:
if (( RANDOM % 2 )); then