Bash comparisions Flashcards
Syntax for comparing variable with a value
Always use double quotations with the variable.
e.g. “$myvariable” -eq “this is a string”
This invariably avoids errors. Whereas sometimes quotations are necessary and omitting them generates errors.
Zero LENGTH string
Non zero LENGTH string
- z “$mystring”
- nz “$mystring”
String equality
String inequality
Numeric equality
Numeric inequality
Numeric less than
Numeric less or equal
Numeric greater than
Numeric greater or equal
“abc” =“$mystring”
“abc” !=“$mystring”
3 -eq“$mystring”
3 -ne“$mystring”
3 -lt“$mystring”
3 -le“$mystring”
3 -gt“$mystring”
3 -ge“$mystring”
Exists and is a regular file
Exists and is a directory
1st file newer than second file
1st file older than second file
-f“$myfile”
-d“$mydir”
“$myfile” -ot./bashrc
“$myfile” -nt./bashrc