Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare variable with integer in shell? [duplicate]

Tags:

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!

like image 887
David Wright Avatar asked Aug 08 '13 18:08

David Wright


People also ask

How do I compare two integer variables in bash?

You can take the same or different values as per your choice. Then we have initialized the “if” statement to contrast the two variables by an operator “-eq”. This will check whether the two variables are equal or not. If the two variables are equal, it will show the message displayed within the first echo phrase.

How do I compare two values in bash?

When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.


1 Answers

Well that is quite simple:

if [ "$counter" -gt 5 ] then     echo "something" fi 
like image 175
konsolebox Avatar answered Oct 16 '22 06:10

konsolebox