You are previewing The Linux Command Line.

The Linux Command Line

Cover of The Linux Command Line by William E. Shotts Jr. Published by No Starch Press
O'Reilly logo

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

The best content for your career. Discover unlimited learning on demand for around $1/day.