Bash Test Operators
STRINGS:
-------------------------------------
syntax:
if [ "$str1" operator "$str2" ]
then
command
fi
-------------------------------------
= is equal to
== is equal to if (( $1 == $2 )) [Note: Used within double parentheses]
The == comparison operator behaves differently within a double-brackets test than within single brackets.
[[ $a == z* ]] True if $a starts with an “z” (pattern matching).
[[ $a == "z*" ]] True if $a is equal to z* (literal matching).
[ $a == z* ] File globbing and word splitting take place.
[ "$a" == "z*" ] True if $a is equal to z* (literal matching).
!= is not equal to
< is less than, in ASCII alphabetical order
> is greater than, in ASCII alphabetical order
-n string is not “null.”
-z string is “null, ” that is, has zero length
INTEGERS:
--------------------------------------------
syntax:
if [ "$string1" operator "$string2" ]
then
command
fi
---------------------------------------------
-eq is equal to if [ $1 -eq 200 ]
-ne is not equal to if [ $1 -ne 1 ]
-gt is greater than if [ $1 -gt 15 ]
-ge is greater than or equal to if [ $1 -ge 10 ]
-lt is less than if [ $1 -lt 5 ]
-le is less than or equal to if [ $1 -le 0 ]
< is less than (within double parentheses)
<= is less than or equal to (within double parentheses)
> is greater than (within double parentheses)
>= is greater than or equal to (within double parentheses)
FILES/DIRECTORIES:
-----------------------------------
syntax:
if [ -operator "$filename" ]
then
command
fi
-------------------------------------
-e file exists
-f file is a regular file (not a directory or device file)
-s file is not zero size (lowercase ‘s’)
-S file is a socket
-d file is a directory
-b file is a block device (floppy, cdrom, etc.)
-c file is a character device (keyboard, modem, sound card, etc.)
-p file is a pipe
-h file is a symbolic link
-L file is a symbolic link
-t file (descriptor) is associated with a terminal device
-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id (sgid) flag set on file or directory
-u set-user-id (suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
f1 -nt f2 file f1 is newer than f2
f1 -ot f2 file f1 is older than f2
f1 -ef f2 files f1 and f2 are hard links to the same file
-------------------------------------
syntax:
if [ "$str1" operator "$str2" ]
then
command
fi
-------------------------------------
= is equal to
== is equal to if (( $1 == $2 )) [Note: Used within double parentheses]
The == comparison operator behaves differently within a double-brackets test than within single brackets.
[[ $a == z* ]] True if $a starts with an “z” (pattern matching).
[[ $a == "z*" ]] True if $a is equal to z* (literal matching).
[ $a == z* ] File globbing and word splitting take place.
[ "$a" == "z*" ] True if $a is equal to z* (literal matching).
!= is not equal to
< is less than, in ASCII alphabetical order
> is greater than, in ASCII alphabetical order
-n string is not “null.”
-z string is “null, ” that is, has zero length
INTEGERS:
--------------------------------------------
syntax:
if [ "$string1" operator "$string2" ]
then
command
fi
---------------------------------------------
-eq is equal to if [ $1 -eq 200 ]
-ne is not equal to if [ $1 -ne 1 ]
-gt is greater than if [ $1 -gt 15 ]
-ge is greater than or equal to if [ $1 -ge 10 ]
-lt is less than if [ $1 -lt 5 ]
-le is less than or equal to if [ $1 -le 0 ]
< is less than (within double parentheses)
<= is less than or equal to (within double parentheses)
> is greater than (within double parentheses)
>= is greater than or equal to (within double parentheses)
FILES/DIRECTORIES:
-----------------------------------
syntax:
if [ -operator "$filename" ]
then
command
fi
-------------------------------------
-e file exists
-f file is a regular file (not a directory or device file)
-s file is not zero size (lowercase ‘s’)
-S file is a socket
-d file is a directory
-b file is a block device (floppy, cdrom, etc.)
-c file is a character device (keyboard, modem, sound card, etc.)
-p file is a pipe
-h file is a symbolic link
-L file is a symbolic link
-t file (descriptor) is associated with a terminal device
-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id (sgid) flag set on file or directory
-u set-user-id (suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
f1 -nt f2 file f1 is newer than f2
f1 -ot f2 file f1 is older than f2
f1 -ef f2 files f1 and f2 are hard links to the same file
Comments
Post a Comment