LinuxCommandLibrary

]

Terminate conditional expressions or lists

TLDR

View documentation for the [ keyword

$ tldr [
copy

SYNOPSIS

[ expression ]

PARAMETERS

-a file
    True if file exists and is a regular file.

-b file
    True if file exists and is a block special file.

-c file
    True if file exists and is a character special file.

-d file
    True if file exists and is a directory.

-e file
    True if file exists.

-f file
    True if file exists and is a regular file.

-g file
    True if file exists and its set-group-ID bit is set.

-h file or -L file
    True if file exists and is a symbolic link.

-k file
    True if file exists and its sticky bit is set.

-p file
    True if file exists and is a named pipe (FIFO).

-r file
    True if file exists and is readable.

-s file
    True if file exists and has a size greater than zero.

-t fd
    True if file descriptor fd is open and refers to a terminal.

-u file
    True if file exists and its set-user-ID bit is set.

-w file
    True if file exists and is writable.

-x file
    True if file exists and is executable.

-O file
    True if file exists and is owned by the effective user ID.

-G file
    True if file exists and is owned by the effective group ID.

file1 -nt file2
    True if file1 is newer than file2 (according to modification date).

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

file1 -ef file2
    True if file1 and file2 refer to the same file.

-z string
    True if the length of string is zero.

-n string
    True if the length of string is non-zero.

string
    Equivalent to -n string.

string1 = string2
    True if the strings string1 and string2 are equal.

string1 == string2
    True if the strings string1 and string2 are equal.

string1 != string2
    True if the strings string1 and string2 are not equal.

arg1 OP arg2
    OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. arg1 and arg2 may be positive or negative integers.

! expression
    True if expression is false.

expression1 -a expression2
    True if both expression1 and expression2 are true.

expression1 -o expression2
    True if either expression1 or expression2 is true.

DESCRIPTION

The `[` command (also known as the `test` command) is used to evaluate conditional expressions in shell scripts. It checks for file types, string equality/inequality, arithmetic conditions, and boolean combinations. It returns an exit status of 0 if the expression is true, and 1 if false. The `]` character is actually the final argument to the `[` command, requiring a space before it. Because of its importance for shell programming, it's usually built into shells for increased performance.

CAVEATS

Remember the space after the opening bracket and before the closing bracket. `[ condition ]` is correct; `[condition]` is not. Also, string comparisons rely on the current locale's collating sequence, which can lead to unexpected behavior if not considered.

EXIT STATUS

Returns 0 if the expression evaluates to true, and 1 if it evaluates to false.

STRING COMPARISONS AND QUOTING

When performing string comparisons, it's crucial to quote variables to prevent word splitting and globbing. For example: `[ "$VAR" = "value" ]`.

HISTORY

The `test` command, and its bracket alias `[`, is a very old utility, dating back to the early days of Unix. Its primary purpose was to provide conditional logic for shell scripts, allowing programs to make decisions based on the state of the system and user input. It became an integral part of standard shell scripting and has been carried forward through various Unix and Linux versions, remaining fundamentally unchanged for backwards compatibility.

SEE ALSO

test(1), bash(1)

Copied to clipboard