Last active
August 6, 2020 21:41
-
-
Save sheilambadi/069b8bb719ed1a71fab0f80adf388ab1 to your computer and use it in GitHub Desktop.
Logical expressions in bash
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Logical expression, Meaning | |
String comparion, | |
if [ -n "$variable" ], Returns true if the variable is empty/null | |
if [ -z "$variable" ], Returns true if the variable is not empty/null | |
if [ "$variable1" = "$variable2" ], Returns true if variable 1 is equal to variable 2 | |
if [ "$variable1" == "$variable2" ], Returns true if variable 1 is equal to variable 2 | |
if [ "$variable1" != "$variable2" ], Returns true if variable 1 is not equal to variable 2 | |
if [ "$variable1" \< "$variable2" ], Returns true if variable 1 is less than variable 2 in ASCII. The < is escaped (\<) | |
if [ "$variable1" \> "$variable2" ], Returns true if variable 1 is greater than variable 2 in ASCII. | |
Integer comparison, | |
if [ $variable -eq 0 ], Returns true if the variable is the equal to zero | |
if [ $variable -ne 0 ], Returns true if the variable is not equal to zero | |
if [ $variable -gt 0 ], Returns true if the variable is greater than zero | |
if [ $variable -lt 0 ], Returns true if the variable is less than zero | |
if [ $variable -ge 0 ], Returns true if the variable is greater than or equal to zero | |
if [ $variable -le 0 ], Returns true if the variable is less than or equal to zero | |
Files and directories, | |
if [ -f "$variable" ], Returns true if the variable is the name of a file | |
if [ ! -f "$variable" ], Returns true if the variable is not the name of a file | |
if [ -d "$variable" ], Returns true if the variable is a directory | |
Combining conditions, | |
if [ "$variable1" = "true" -o "$variable1" = "false" ], Returns true if variable 1 is equal to true or false | |
if [ $variable1 -gt 3 -a $variable1 -lt 1 ], Returns true if variable 1 is greater than 3 and less than 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment