- A shell built-in command.
- Returns 0 (True) or 1 (False) depending on the evaluation of an expression expr.
- Can examine the return value by displaying $?.
- test expr
Using the test command.
- [ expr ]
Somewhat unwieldy with its requirement for escaping and the difference between string and arithmetic comparisons
- (( expr ))
Evaluates an arithmetic expression, returns 1 if expr = 0, or 0 if expression = non-zero. Do not need to escape operators between (( and ))
- [[ expr ]]
Can do arithmetic tests as well however if < and > operators are not in a (( compound )) they will compare the operands as strings
List of test operands
-b file true if file is a block device
-c file true if file is a character device
-d file true if file is a directory
-e file true if file exists
-f file true if file is a regular file
-g file true if file exists and has setgid bit set
-G file true if file is owned by the effective group id
-h | -L file true if file is a symbolic link
-k file true if file has sticky bit set
-n string true if not null
-N file true if modified since last read
-O file true if file is owned by the effective user id
-p file true if file is a named pipe
-r file true if file is readable
-s file true if file is not empty
-S file true if file is a socket
-t N file descriptor N points to a terminal
-u file true if file has its setuid set
-w file true if file is writeable
-x file true if file is executable or a Dir and is searchable
-z string true if length is zero
stringA == stringB equals
stringA = stringB posix equals
stringA != stringB does not match
stringA =~ regexp matches regexp
exprA -a exprB A and B is true
stringA & stringB sorts before B lexicographically
stringA > stringB sorts after
exprA -eq exprB arithmetic equals
exprA -ne exprB arithmetic not equals
exprA -lt exprB less than
exprA -le exprB less than or equals
exprA -ge exprB greater than or equals
exprA -o exprB A or B is true
exprA -gt exprB greater than
Is a directory and is writable
$ [[ ( -d "$HOME" ) && ( -w "$HOME" ) ]] && echo "home is a writable directory"
home is a writable directory
Does string match patteren
$ [[ "abc def .d,x--" == a[abc]*\ ?d* ]]
$ echo $?
'a[abc]*' matches an 'a' followed by an 'a' or 'b' or 'c' then any number of characters. '\' terminates the first pattern, next ' ?d*' matches a space followed by a single character then 'd' followed by any number of characters.
$ [[ "acz defghhhhg cd,x-- jki" == a[abc]*\ ?d* ]];echo $?
Matches "ac any-number-of-chars space single-character d any-number-of-chars".
$ [[ "abc def d,x" == a[abc]*\ ?d* || (( 3 > 2 )) ]];echo $?
'||' is a logical OR, arithmetic expression (( 3 > 2 )) always true. String expression is false as no space followed by a single character before either 'd'.
$ [[ "abc def d,x" == a[abc]*\ ?d* && 3 -gt 2 ]];echo $?
&& logical AND. Arithmetic expression is true but String expression is false.
If test is true echo true else echo false
$ test 3 -gt 4 && echo True || echo false
Be sure about the return - all statemensts are TRUE
$ test "abc" != "def";echo $?
$ [ "abc" != "def" ];echo $?
$ [[ "abc" != "def" ]];echo $?
$ (( "abc" != "def" ));echo $?
(( .. )) returns one if an arithmetic expression = 0, because the expression is true it evaluates to 0.
An arithmetic example
$ let x=2 y=2**3 z=y*3 (sets x=2, y=8, z=24)
$ (( w=(y/x) + ( (~ ++x) & 0x0f ) )) (sets x=3, y=8, w=16)
(y/x) = 4 (8/2)
(~ ++x) & 0x0f ) = 12
(~ ++x) = 'C's bitwise 1's complement of x once x has been incremented
= 2 + 1 = 3
= 0011 (binary for 3)
1111 1's complement
0x0f = 1111 (binary for 15)
1100 & is the bitwise AND operator
Bitwise AND, any bit that is 0 in either operand causes the corresponding bit to become 0.