Control Structures
suggest changeSyntax
[ "$1" = "$2" ]—[is actually a command, so it requires a space before and after it.test "$1" = "$2"—testis a synonym for the[command.
Parameters
Parameter to [ or test |
Details |
|---|---|
| File operators | |
-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 comparators | |
-z "$str" |
True if the length of the string is zero. |
-n "$str" |
True if the length of the string is non-zero. |
"$str" = "$str2" |
True if $str is equal to $str2. Not best for integers. It may work, but it will be inconsistent. |
"$str" != "$str2" |
True if the strings are not equal. |
| Integer comparators | |
"$int1" -eq "$int2" |
True if the integers are equal. |
"$int1" -ne "$int2" |
True if the integers are not equal. |
"$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. |
Remarks
There are many comparator parameters available in bash. Not all are yet listed here.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents