[shell] difference “=” and “-eq”

Question : the difference between “=” and “-eq” ?

See this code :

#!/bin/bash
# '='
if [ 01 = 1 ] ; then
  echo "01 = 1 OK"
else
  echo "01 = 1 KO"
fi
# '-eq'
if [ 01 -eq 1 ] ; then
  echo "01 -eq 1 OK"
else
  echo "01 -eq 1 KO"
fi

Guess the result of this code :

01 = 1 KO
01 -eq 1 OK

Why that ?
In fact, “=” (which can be also wrote ==) is a string comparison;
“-eq” is a numeric comparison.

Thank you Idriss for information !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s