0

I'm a newbie of bash. When I try to use the command [[ for compare, I encounter the problem as below:

enter image description here

[root@JD ~]# [[ 2 <= 2 ]]
-bash: syntax error in conditional expression
-bash: syntax error near `2'

From my tutorial book, [ 2 -le 2 ] should be equal to [[ 2 <= 2 ]], I don't know the reason why it's a syntax error.

jww
  • 97,681
  • 90
  • 411
  • 885
Wook Gus
  • 1
  • 2
  • Please replace image with text. – Cyrus Jun 16 '18 at 11:02
  • 1
    Particularly if you are just learning bash and therefore expect to make mistakes, I'd strongly recommend that you avoid doing everything as root, since without protection your mistakes could be a lot more serious. (I'd make the same suggestion to an experienced user, but I suspect most experienced users have already learned this lesson, possibly the hard way.) – rici Jun 16 '18 at 14:46
  • 1
    Possible duplicate of [Comparing numbers in Bash](https://stackoverflow.com/q/18668556/608639), [Comparing integers: arithmetic expression or conditional expression](https://unix.stackexchange.com/q/278707/56041), [Compare variable with integer in shell?](https://stackoverflow.com/q/18133161/608639), etc. – jww Jun 16 '18 at 15:07

1 Answers1

1

In double square brackets, <= is not a recognised operator. < and > are used to compare strings, anyway. Use -le for numbers, or switch to arithmetic expression, which uses round parentheses:

(( 5 <= 10 ))
choroba
  • 231,213
  • 25
  • 204
  • 289