1

In a Bash script I want to check if var1 is located between var2 and var3. But I can't find how to do it.

Something like this.

var1=15
var2=10
var3=20

if [ "$var1" is located beetween "$var2" and "$var3" ]
then
    echo "ok"
else
    echo "not ok"
fi

Can you help me please ?

Thank you.

1
  • 1
    What do you mean by "located between"? Value? If so, man test. If something else, please elaborate. Commented Feb 14, 2021 at 15:30

2 Answers 2

1

Assuming that the values are always numeric and by "var1 located between var2 and var3" you mean that var2 < var1 < var3, you can use this:

if (( $var2 < $var1 )) && (( $var1 < $var3 )); then
    echo "ok"
else
    echo "not ok"
fi
1
  • thank you everything works with your example adapted to my script
    – Maxime
    Commented Feb 17, 2021 at 17:46
-2

Your logic is pointless... After all the code in the script is static, so when you change anything in the variables, you can adjust the logic in the rest of the script.

2
  • It's probably a fair assumption that was a sample script to illustrate his question rather than his actual script. Commented Feb 15, 2021 at 7:49
  • That's an assumption and I still think that the question is poorly written. Commented Feb 15, 2021 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .