Skip to content

Bash test operator

File type tests

These options test for particular types of files. All cases will only return True (0) if the file exists.
`-b file’
True if file is a Block special device.

`-c file’
True if file is a Character special device.

`-d file’
True if file is a Directory.

`-e file’
True if file Exists.

`-f file’
True if file is a regular File.

`-g file’
True if file has its set-group-id bit set.

`-G file’
True if file is owned by the current effective group id.

`-k file’
True if file has its “sticky” bit set.

`-h file’
`-L file’
True if file is a symbolic Link.

`-O file’
True if file is owned by the current effective user id.

`-p file’
True if file is a named Pipe.

`-r file’
True if file is readable.

`-S file’
True if file is a Socket.

`-s file’
True if file has a Size greater than zero.

`-t [FD]’
True if FD is opened on a terminal. If FD is omitted, it defaults
to 1 (standard output).

`-u file’
True if file has its set-user-id bit set.

`-w file’
True if file is writable.

`-x file’
True if file is executable.

`file1 -ef file2′
True if file1 and file2 have the same device and inode numbers,
i.e., if they are hard links to each other.

File Age
These options test the file modification date.
`file1 -nt file2′
True if file1 is newer than file2.

`file1 -ot file2′
True if file1 is older than file2.

String tests
These options test string characteristics. Strings are not quoted for `test’, though you may need to quote them to protect characters with special meaning to the shell, e.g., spaces.
`-z String’
True if the length of String is zero.

`-n String’
`String’
True if the length of String is nonzero.

`String1 = String2′
True if the strings are equal.

`String1 != String2′
True if the strings are not equal.

Numeric tests
Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special expression `-l STRING’, which evaluates to the length of STRING.

`ARG1 -eq ARG2′
True if ARG1 is equal than ARG2
`ARG1 -ne ARG2′
True if ARG1 is not-equal than ARG2
`ARG1 -lt ARG2′
True if ARG1 is less-than than ARG2
`ARG1 -le ARG2′
True if ARG1 is less-than-or-equal than ARG2
`ARG1 -gt ARG2′
True if ARG1 is greater-than than ARG2
`ARG1 -ge ARG2′
True if ARG1 is greater-than-or-equal than ARG2