Text-only Table of Contents (frame/ no frame)
(17) Conditional Tests (contd.) Previous Top Next

More conditional tests for [...] and [[...]] commands

Arithmetic tests

$variable -eq number
True if $variable, interpreted as a number, is equal to number.
$variable -ne number
True if $variable, interpreted as a number, is not equal to number.
Similarly, -lt = less than, -le = less than or equal, -gt = greater than, -ge = greater than or equal

Additional tests for [[...]] (ksh and bash)

$variable = pattern
True if $variable matches pattern. If pattern contains no wildcards, then this is just an exact text match. The same wildcards as used for filename matching are used. Less detail
The pattern must not be quoted. Since [[...]] is internal to the shell, the pattern in this case is treated differently and not filename-expanded as an external command would require.
file1 -nt file2
True if file1 is newer than file2.
Similarly -ot = older than
file1 -ef file2
true if file1 is effectively the same as file2, after following symlinks and hard links.

Negating and Combining tests

Tests may be negated by prepending the ! operator, and combined with boolean AND and OR operators using the syntax:
conditional -a conditional, conditional -o conditional
AND and OR syntax for test and [

conditional && conditional, conditional || conditional
AND and OR syntax for [[ ... ]]
Parentheses may be inserted to resolve ambiguities or override the default operator precedence rules.

Examples:

if [[  -x /usr/local/bin/lserve && \
       -w /var/logs/lserve.log ]]; then
   /usr/local/bin/lserve >> /var/logs/lserve.log &
fi

pwent=`grep '^richard:' /etc/passwd`
if [ -z "$pwent" ]; then
   echo richard not found
fi

Previous Top Next


cond-tests2.src  last modified Mar 11, 2005 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College