| Boolean | |
|---|---|
Is true * |
VAR=: if [[ $VAR ]] |
| Is false | VAR= if [[ ! $VAR ]] |
| String | |
| Is equal | if [[ $VAR1 == $VAR2 ]] |
| Not equal | if [[ $VAR1 != $VAR2 ]] |
| Is empty / zero length | if [[ -z $VAR ]] |
| Not empty / non-zero length | if [[ -n $VAR ]] |
| Less than (ASCII alpha) | if [[ $VAR1 < $VAR2 ]] |
| Greater than (ASCII alpha) | if [[ $VAR1 > $VAR2 ]] |
Regular expression ** |
if [[ $VAR =~ ^my.+regexp$ ]] |
Regular expression not ** |
if [[ ! $VAR =~ ^my.+regexp$ ]] |
String contains *** |
if [[ $VAR =~ "needle" ]] |
| Integer | |
| Is equal | if [[ $VAR1 -eq $VAR2 ]] |
| Not equal | if [[ $VAR1 -ne $VAR2 ]] |
| Less than | if [[ $VAR1 -lt $VAR2 ]] |
| Less than or equal | if [[ $VAR1 -le $VAR2 ]] |
| Greater than | if [[ $VAR1 -gt $VAR2 ]] |
| Greater than or equal | if [[ $VAR1 -ge $VAR2 ]] |
| File system | |
| Is file (file, dir, symlink, anything) | if [[ -e $FILE_PATH ]] |
| Is regular file | if [[ -f $FILE_PATH ]] |
| Is regular file, is readable | if [[ -r $FILE_PATH ]] |
| Is regular file, is writable | if [[ -w $FILE_PATH ]] |
| Is regular file, is executable | if [[ -x $FILE_PATH ]] |
| Is regular file, not zero size | if [[ -s $FILE_PATH ]] |
| Is directory | if [[ -d $FILE_PATH ]] |
| Is symlink | if [[ -h $FILE_PATH ]] |
| Is socket | if [[ -S $FILE_PATH ]] |
| File compare modification/exists | |
Is FILE1 newer than FILE2...or FILE1 exists and FILE2 does not
|
if [[ $FILE1 -nt $FILE2 ]] |
Is FILE1 older than FILE2...or FILE2 exists and FILE1 does not
|
if [[ $FILE1 -ot $FILE2 ]] |
| Combine expressions | |
If EXPR1 and EXPR2 are true |
if [[ (EXPR1) && (EXPR2) ]] |
If EXPR1 and EXPR2 are true |
if [ (EXPR1) -a (EXPR2) ] |
If EXPR1 or EXPR2 are true |
if [[ (EXPR1) || (EXPR2) ]] |
If EXPR1 or EXPR2 are true |
if [ (EXPR1) -o (EXPR2) ] |
*: Use of:here as a NOP operation, which will always return true.**: Regular expression captures available via array${BASH_REMATCH[*]},${BASH_REMATCH[1]}, etc.***: This behavior for the=~operator was introduced between Bash3.1->3.2.
- Most lifted from here: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
- Difference between
[ ]and[[ ]]: https://mywiki.wooledge.org/BashFAQ/031 - Bash NOP (no op)
:: https://en.wikipedia.org/wiki/NOP#Shell_Scripting_.28bash.2C_zsh.2C_etc.29 - GNU Bash reference manual: https://www.gnu.org/software/bash/manual/bashref.html