“Command not found” when attempting integer equality in bash

后端 未结 3 505
我在风中等你
我在风中等你 2020-12-03 15:11

Ok, this is probably going to be ultra obvious to anyone that has spent more time with bash than I have.

I\'m trying to run this code:

#!/bin/bash

i         


        
相关标签:
3条回答
  • 2020-12-03 15:29

    Try adding spaces around your brackets:

    if [ "1" -eq "2" ]
    
    0 讨论(0)
  • 2020-12-03 15:30

    You need to add a space after the [ and before the ] like so:

    if [ "1" -eq "2" ]
    

    However, that way is deprecated and the better method to use is:

    #!/bin/bash
    
    if ((1 == 2)) 
    then
        echo "True"
    else
        echo "False"
    fi
    
    0 讨论(0)
  • 2020-12-03 15:35

    yep eq is used only for arithmetic comparaisons.

    for string comparison you have to use =

    #!/bin/bash
    
    if [ "1" = "2" ] 
    then
        echo "True"
    else
        echo "False"
    fi
    

    plus you need some space around the brackets.

    0 讨论(0)
提交回复
热议问题