Bash Scripting - String and Numeric Comparisons

 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 –

OperatorDescription
==Returns true if the strings are equal
!= Returns true if the strings are not equal
-nReturns true if the string to be tested is not null
-zReturns 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 –

OperatorDescription
-eqEqual
-geGreater Than or Equal
-gtGreater Than
-leLess Than or Equal
-ltLess Than
-neNot 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 – 

Comments

Popular posts from this blog

Different Types of Reports in Scrum - Agile

Terraform

Scrum Master Interview help - Bootcamp