How do I do if statement arithmetic in bash?

后端 未结 5 2014
感情败类
感情败类 2020-12-04 11:16

I want to do something like this:

if [ $1 % 4 == 0 ]; then
...

But this does not work.

What do I need to do instead?

5条回答
  •  醉话见心
    2020-12-04 11:44

    single brackets ([..]) don't work for some tests, try with double brackets ([[...]]) and enclose the mod in ((..)) to evaluate the % operator properly:

    if [[ $(( $1 % 4 )) == 0 ]]; then
    

    More details here:
    http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_02.html

提交回复
热议问题