Comparing numbers in Bash

前端 未结 8 1736
栀梦
栀梦 2020-11-22 02:03

I\'m starting to learn about writing scripts for the bash terminal, but I can\'t work out how to get the comparisons to work properly. The script I\'m using is:



        
8条回答
  •  逝去的感伤
    2020-11-22 02:52

    In bash, you should do your check in arithmetic context:

    if (( a > b )); then
        ...
    fi
    

    For POSIX shells that don't support (()), you can use -lt and -gt.

    if [ "$a" -gt "$b" ]; then
        ...
    fi
    

    You can get a full list of comparison operators with help test or man test.

提交回复
热议问题