The
string comparison means in the shell scripting we can take decisions by doing
comparisons within strings as well. Here is a descriptive table with all the
operators --
| Operator | Description |
|---|---|
| == | Returns true if the strings are equal |
| != | Returns true if the strings are not equal |
| -n | Returns true if the string to be tested is not null |
| -z | Returns true if the string to be tested is null |
Arithmetic
operators are used for checking the arithmetic-based conditions. Like less
than, greater than, equals to, etc. Here is a descriptive table with all the
operators --
| Operator | Description |
|---|---|
| -eq | Equal |
| -ge | Greater Than or Equal |
| -gt | Greater Than |
| -le | Less Than or Equal |
| -lt | Less Than |
| -ne | Not Equal |
Below is a
simple example of the same –
Example Script:
if [ 10 -eq 10 ];then
echo "Equal"
fi
if [ 'Geeks' == 'Geeks' ];
then
echo "same" #output
else
echo "not same"
fi
Output of
String and Numeric Comparisons:
Equal
same
In this
example first one (-eq )is a numeric comparison that checks for equality.
The second one ( == ) is also check for equality but in strings. Below is
the terminal shell pictorial depiction after executing the following script --

No comments:
Post a Comment