23

I have an if statement I need to run, as long as the value I have stored in my $counter variable is greater than 5.

Here is the respective section of my current (non-functioning) script:

if $counter > 5
then
    echo "something"
fi

The mistake I'm making is probably very obvious, but for some reason I couldn't find the solution online.. Thanks!

David Wright
  • 425
  • 3
  • 8
  • 16

2 Answers2

49

Well that is quite simple:

if [ "$counter" -gt 5 ]
then
    echo "something"
fi
konsolebox
  • 72,135
  • 12
  • 99
  • 105
26

Arithmetic needs to be done between (( and )):

if (( $counter > 5 ))

Incidentally, you can also leave off the $ in arithmetic, though it doesn't hurt to keep it.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Yes however, I would argue conditional expressions should be done in `[[ ]]`. So which one should really be it? I'd pick `[[ ]]`. See my arguments here: http://stackoverflow.com/a/18568726/445221. (Commenting for reference). – konsolebox Nov 05 '15 at 19:25