Many things can be tested for natively in Bash, without the need to make fancy if statements. More information can be found by running "man test
" but here are the most interesting ones:
- File Existance
[ -f filename ]
- File is a link
[ -L filename ]
OR [ -h filename ]
(Both are equivalent)
- Variable is not empty
[ -n var ]
- File is not empty
[ -s filename ]
- Greater Than
[ var1 -gt var2 ]
- Greater or equal
-
[ var1 -ge var2 ]
- Integer Equality
[ int1 -eq int2 ]
- Lower Than
[ var1 -lt var2 ]
- Lower or equal
-
[ var1 -le var2 ]
- Non-null string
[ -z var]
- String Equality
[ str1 = str2 ]
(Can also use [ str1 == str2 ]
, with slightly different implications)
For testing two different things, you can use:
&&
- With the meaning of "and", to check that both are true
||
- With the meaning of "or", to check if either of the conditions are true