Lesson 1 of 0
In Progress
Test Operators
Integer Test operators | Meaning |
---|---|
[ <int1> -eq <int2> ; echo $? |
equal |
[ <in1> -ne <int2> ; echo $? |
not equal |
[ <in1> -gt <int2> ; echo $? |
greater than |
[ <in1> -lt <int2> ; echo $? |
less than |
[ <in1> -geq <int2> ; echo $? |
greater than or equal to |
[ <in1> -leq <int2> ; echo $? |
less than or equal to. |
💡 The following only work on strings and not values of integers. It will treat numbers as plain text strings.
String Test operators | Meaning |
---|---|
[[ $string1 = $string2 ]] ; echo $? |
compares two strings and will exit 0 (success) if they are the same. Other languages compare using “==” or “===” |
[[ $string1 != $string2 ]] ; echo $? |
compares two strings and will exit 1 (fail) if they are the same. This != is pretty common throughout other languages. |
[[ -z $some_variable ]] ; echo $? |
tests if a string is empty, this is commonly used to see if a file exists or not. If it’s null or “none” then it will return an exit of 0. |
[[ -n $some_variable ]] ; echo $? |
Does the opposite of the -z operator in that it will exit 1 if $some_variable is null or “none”. (It’s checking to see if the string is not empty) |
File Test operators | Meaning |
---|---|
[[ -e $file_name ]] ; echo $? |
This operator will exit 0 if the file specified “exists” or not. |
[[ -f $file_name ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the specified file is a “file” and not a directory. If it’s a directory it will exit 1 |
[[ -d $file_name ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the parameter passed is a directory. |
[[ -x $file_name ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the file specified has executable permissions. |
[[ -r $file_name ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the file specified has readable permissions. |
[[ -w $file_name ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the file specified has writable permissions. |
[[ $file_name1 -nt $file_name2 ]] ; echo $? |
This operator checks to see if a file exists and will exit 0 if the file specified is newer than. |