Comparing numbers in Bash

前端 未结 8 1906
栀梦
栀梦 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:42

    Plain and simple

    #!/bin/bash
    
    a=2462620
    b=2462620
    
    if [ "$a" -eq "$b" ];then
      echo "They're equal";
    fi
    

    You can check out this cheatsheet if you want more number comparsions in the wonderful world of Bash Scripting.

    Shortly, integers can only be compared with:

    -eq # equal
    -ne # not equal
    -lt # less than
    -le # less than or equal
    -gt # greater than
    -ge # greater than or equal
    

提交回复
热议问题