O'Reilly logo

Mac OS X Tiger in a Nutshell by Jason McIntosh, Chuck Toporek, Chris Stone, Andy Lester

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

test

Synopsis

test expression [expression]

Also exists as a built-in in most shells.

Evaluates an expression and, if its value is true, returns a zero exit status; otherwise, returns a nonzero exit status. In shell scripts, you can use the alternate form [ expression ]. This command is generally used with conditional constructs in shell programs.

File testers

The syntax for all of these options is test option file. If the specified file doesn't exist, the testers return false. Otherwise, they test the file as specified in the option description.

-b

Is the file block special?

-c

Is the file character special?

-d

Is the file a directory?

-e

Does the file exist?

-f

Is the file a regular file?

-g

Does the file have the set-group-ID bit set?

-G

Is the file owned by the process's effective GID?

-k

Does the file have the sticky bit set?

-L

Is the file a symbolic link?

-n

Is the string of nonzero length?

-O

Is the file owned by the process's effective UID?

-p

Is the file a named pipe?

-r

Is the file readable by the current user?

-s

Is the file nonempty?

-S

Is the file a socket?

-t [file-descriptor]

Is the file associated with file-descriptor (or 1, which is standard output, by default) connected to a terminal?

-u

Does the file have the set-user-ID bit set?

-w

Is the file writable by the current user?

-x

Is the file executable?

File comparisons

The syntax for file comparisons is test file1 option file2. A string by itself, without options, returns true if it's at least one character long.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required