Home > other >  How to say less than but no equal to in bash?
How to say less than but no equal to in bash?

Time:10-10

I'm getting an error with this, I did my research but found nothing.

if [ $value -lt 3 -ne 1 ]; then
execute code
fi
line 6: [: syntax error: -ne unexpected

CodePudding user response:

I like to switch to arithmetic expressions using (( when I need tests like these:

declare -a values=(1 2 3)

for value in "${values[@]}"; do
  if (( value != 1 && value < 3 )); then
    echo "execute code for $value"
  fi
done

The above outputs:

execute code for 2

CodePudding user response:

One way to make this work is

if [ $value -lt 3 ] && [ $value -ne 1 ]; then
echo "Hello"
fi

CodePudding user response:

use (( )) brackets for arithmetic operations and [[ ]] for strings comparison $ is redundant in round brackets so (( $a == 1 )) is the same as (( a == 1 ))

typeset a=2

(( a < 3 )) && (( a != 1 )) && echo "Execute code"

more details : http://faculty.salina.k-state.edu/tim/unix_sg/bash/math.html

  • Related