Stats

170 Contributors: 10
2017-06-11
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Control Structures

Download bash eBook

Syntax

  • [ "$1" = "$2" ] #A "[" bracket is actually a command. Because of this it requires a space befor and after it.
  • test "$1" = "$2" #Test is a synonym for the "[" command

Parameters

Parameter to [ or testDetails
File OperatorsDetails
-e "$file"Returns true if the file exists.
-d "$file"Returns true if the file exists and is a directory
-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 ComparatorsDetails
-z "$str"True if length of string is zero
-n "$strTrue if length of string is non-zero
"$str" = "$str2"True if string $str is equal to string $str2. Not best for integers. It may work but will be inconsitent
"$str" != "$str2"True if the strings are not equal
Integer Comparators      Details
"$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 int 2
"$int1" -ge "$int2"True if int1 is greater than or equal to int2
"$int1" -lt "$int2"True if int1 is less than int 2
"$int1" -le "$int2"True if int1 is less than or equal to int2

Remarks

There are many comparator parameters available in bash. Not all are yet listed here.

Related Examples