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

Conditional tests for [...] and [[...]] commands

Most of the useful flow-control operators involve making some conditional test and branching on the result (true/false). The test can be either the test command, or its alias, [, or the ksh/bash built-in [[ ... ]] command, which has slightly different options, or it can be any command which returns a suitable exit status. Zero is taken to be "True", while any non-zero value is "False". Note that this is backwards from the C language convention.

File tests

-e file
True if file exists (can be of any type).
-f file
True if file exists and is an ordinary file.
-d file
True if file exists and is a directory.
-r file
True if file exists and is readable
Similarly, -w = writable, -x = executable, -L = is a symlink.
-s file
True if file exists and has size greater than zero
-t filedescriptor
True if the open filedescriptor is associated with a terminal device. E.g. this is used to determine if standard output has been redirected to a file.

Character string tests

-n "string"
true if string has non-zero length
-z "string"
true if string has zero length More detail
$variable = text
True if $variable matches text.
$variable < text
True if $variable comes before (lexically) text
Similarly, > = comes after

Previous Top Next


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