"Command not found" when trying to integer equality in bash

Well, this is likely to be very obvious to anyone who has spent more time with bash than me.

I am trying to run this code:

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

but when I execute the file, it sends back

 ./test.sh: line 3: 1: command not found False 

There must be something important that I am missing. I saw people use a semicolon after brackets, this does not seem to make any difference ...: S

+8
bash
source share
3 answers

yep eq is used only for arithmetic comparisons.

to compare strings you should use =

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

plus you need space around the brackets.

+5
source share

You need to add a space after [ and before ] as follows:

 if [ "1" -eq "2" ] 

However, this method is deprecated and the best way to use it is:

 #!/bin/bash if ((1 == 2)) then echo "True" else echo "False" fi 
+20
source share

Try adding spaces around the brackets:

 if [ "1" -eq "2" ] 
+8
source share

Source: https://habr.com/ru/post/650235/


All Articles