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. 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
    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. Feb 15, 2021 at 7:49
  • That's an assumption and I still think that the question is poorly written. Feb 15, 2021 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.