## Name test - checks files and compare values ## Synopsis ```**sh $ test expression $ test $ [ expression ] $ [ ] ``` ## Description `test` takes a given expression and sets the exit code according to its truthiness, 0 if true, 1 if false. An omitted expression defaults to false, and an unexpected error causes an exit code of 126. If `test` is invoked as `[`, a trailing `]` is _required_ after the expression. ## Expressions The expression can take any of the following forms: ### Grouping * `( )` value of expression ### Boolean operations * `! ` negation of expression * ` -a ` boolean conjunction of the values * ` -o ` boolean disjunction of the values ### String comparison * `` whether the string is non-empty * `-n ` whether the string is non-empty * `-z ` whether the string is empty * ` = ` whether the two strings are equal * ` != ` whether the two strings not equal ### Integer comparison * ` -eq ` whether the two integers are equal * ` -ne ` whether the two integers are not equal * ` -lt ` whether the integer on the left is less than the integer on the right * ` -gt ` whether the integer on the left is greater than the integer on the right * ` -le ` whether the integer on the left is less than or equal to the integer on the right * ` -ge ` whether the integer on the left is greater than or equal to the integer on the right ### File comparison * ` -ef ` whether the two files are the same (have the same inode and device numbers) * ` -nt ` whether the file on the left is newer than the file on the right (modification date is used) * ` -ot ` whether the file on the left is older than the file on the right (modification date is used) ### File type checks * `-b ` whether the file is a block device * `-c ` whether the file is a character device * `-f ` whether the file is a regular file * `-d ` whether the file is a directory * `-p ` whether the file is a pipe * `-S ` whether the file is a socket * `-h `, `-L ` whether the file is a symbolic link ### File permission checks * `-r ` whether the current user has read access to the file * `-w ` whether the current user has write access to the file * `-x ` whether the current user has execute access to the file * `-e ` whether the file exists Except for `-h/-L`, all file checks dereference symbolic links. NOTE: Your shell might have a builtin named 'test' and/or '[', please refer to your shell's documentation for further details. ## Options None. ## Examples ```sh # Conditionally do something based on the value of a variable $ /bin/test "$foo" = bar && echo foo is bar # Check some numbers $ /bin/test \( 10 -gt 20 \) -o \( ! 10 -ne 10 \) && echo "magic numbers!" ``` ## See Also * [`find`(1)](find.md)