Control Structures in Linux BASH

The Linux BASH provides many operators to check against </b>test<b> keyword
We are going to explain the Conditional comparison Operators for the Keyword parameter to</b><b>test or [

For a detailed explanation of the Keyword test or [ Please refer to our Control Structure: Bash If then Else.

 

File Comparison Operators
Condition Explanation
-d “$file” Returns true if the file exists and is a directory
-e “$file” Returns true if the file exists.
-f “$file” Returns true if the file exists and is a regular file
-h “$file” Returns true if the file exists and is a symbolic link

 

String Comparators Condition  Explanation
“$str” = “$str2” True if string $str is equal to string $str2. Not best for integers.
“$str” != “$str2” True if the strings are not equal
-z “$str” True if length of string is zero
-n “$str” True if length of string is non-zero

 

Integer Comparators Condition Explanation
“$int1” -eq “$int2” True if the integers are equal
“$int1” -ne “$int2” True if the integers are not equals
“$int1” -gt “$int2” True if $int1 is greater than $int2
“$int1” -ge “$int2” True if $int1 is greater than or equal to $int2
“$int1” -lt “$int2” True if $int1 is less than $int2
“$int1” -le “$int2” True if $int1 is less than or equal to $int2

 

Leave a Comment