Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

could someone help me figure out why this doesn't work?

i=0

if [$i -eq 0]
then 
   echo "i is equal to 0"
else 
   echo "NOT EQUAL <><><><><><><><><><><><><><><><><><><>"
fi

it is part of a bash script and it always takes the else branch. I'm completely new to bash so its probably something silly

like image 457
lollercoaster Avatar asked Jan 22 '26 00:01

lollercoaster


1 Answers

you need [ $i instead of [$i.

This is because the [ is a builtin command and $i should be it's first parameter. If you miss the space between command and parameter, then the shell will look for [$i command and after evaluation will tell you that there is no [0 command to be executed.

like image 107
Michał Šrajer Avatar answered Jan 24 '26 22:01

Michał Šrajer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!