Skip to content
Bash

test/conditional API Reference

Bash test expressions — file, string and numeric comparisons used in [ ] and [[ ]] conditionals.

By EZ4Code Team

test/conditional

Test operators for use with the [ (test) command and the more powerful [[ ]] compound command.

[ -f file ]

Returns true if file exists and is a regular file (not a directory or device).

Returns: boolean (exit status)

[ -d dir ]

Returns true if dir exists and is a directory.

Returns: boolean (exit status)

[ -z string ]

Returns true if the length of string is zero (empty).

Returns: boolean (exit status)

[ -n string ]

Returns true if the length of string is non-zero (not empty).

Returns: boolean (exit status)

[ str1 = str2 ]

Returns true if the two strings are equal. Use != for inequality.

Returns: boolean (exit status)

[ num1 -eq num2 ]

Returns true if the two integers are equal. Other numeric ops: -ne, -lt, -le, -gt, -ge.

Returns: boolean (exit status)

[ -e path ]

Returns true if path exists (any type: file, directory, symlink, etc.).

Returns: boolean (exit status)

[[ str =~ regex ]]

Returns true if str matches the extended regular expression regex. Only in [[ ]], not [ ].

Returns: boolean (exit status)

More Bash API References