Using test

By far, the command used most frequently with if is test. The test command performs a variety of checks and comparisons. It has two equivalent forms:

test expression

and the more popular

[ expression ]

where expression is an expression that is evaluated as either true or false. The test command returns an exit status of 0 when the expression is true and a status of 1 when the expression is false.

File Expressions

The expressions in Table 27-1 are used to evaluate the status of files.

Table 27-1. test File Expressions


Is true if . . .

file1 -ef file2

file1 and file2 have the same inode numbers (the two filenames refer to the same file by hard linking).

file1 -nt file2

file1 is newer than file2.

file1 -ot file2

file1 is older than file2

Get The Linux Command Line now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.