TEST(1)

๐Ÿ’… TEST(1) โ€” the test page, bestie edition ๐ŸŽ€

okay so welcome to TEST(1), the manual for test from the General Commands Manual section 1, and honestly? she's a legend. she checks file types and compares values, and she's been carrying every if statement you've ever written. no cap. ๐Ÿ‘‘


โœจ NAME โœจ

test โ€” check file types and compare values. that's it, that's the whole job, and she does it flawlessly ๐Ÿซถ


โœจ SYNOPSIS โœจ

test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION

likeโ€ฆ yes, [ is literally just test wearing a little outfit. same girl, different bracket. ๐Ÿ’…


โœจ DESCRIPTION โœจ

Check file types and compare values. short description, huge impact. she's the vibe check of your shell scripts fr fr. ๐ŸŒธ


โœจ OPTIONS โœจ

  • -h, --help โ€” print help ๐Ÿ’– (yes, -h means help here, but keep reading, because in the file-tests section -h FILE means something completely different and that's just how it is, bestie ๐Ÿ’€)
  • -V, --version โ€” print version โœจ

โœจ EXTRA โœจ

Exit with the status determined by EXPRESSION. that's the whole deal โ€” she speaks in exit codes, and every script listens. ๐Ÿ‘‘

An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of:

  • ( EXPRESSION ) โ€” EXPRESSION is true
  • ! EXPRESSION โ€” EXPRESSION is false
  • EXPRESSION1 -a EXPRESSION2 โ€” both EXPRESSION1 and EXPRESSION2 are true
  • EXPRESSION1 -o EXPRESSION2 โ€” either EXPRESSION1 or EXPRESSION2 is true

๐ŸŒˆ String operations:

  • -n STRING โ€” the length of STRING is nonzero
  • STRING โ€” equivalent to -n STRING
  • -z STRING โ€” the length of STRING is zero
  • STRING1 = STRING2 โ€” the strings are equal
  • STRING1 != STRING2 โ€” the strings are not equal
  • STRING1 > STRING2 โ€” STRING1 is greater than STRING2 in the current locale
  • STRING1 < STRING2 โ€” STRING1 is less than STRING2 in the current locale

๐Ÿงฎ Integer comparisons:

  • INTEGER1 -eq INTEGER2 โ€” INTEGER1 is equal to INTEGER2
  • INTEGER1 -ge INTEGER2 โ€” INTEGER1 is greater than or equal to INTEGER2
  • INTEGER1 -gt INTEGER2 โ€” INTEGER1 is greater than INTEGER2
  • INTEGER1 -le INTEGER2 โ€” INTEGER1 is less than or equal to INTEGER2
  • INTEGER1 -lt INTEGER2 โ€” INTEGER1 is less than INTEGER2
  • INTEGER1 -ne INTEGER2 โ€” INTEGER1 is not equal to INTEGER2

๐Ÿฆ‹ File operations:

  • FILE1 -ef FILE2 โ€” FILE1 and FILE2 have the same device and inode numbers
  • FILE1 -nt FILE2 โ€” FILE1 is newer (modification date) than FILE2
  • FILE1 -ot FILE2 โ€” FILE1 is older than FILE2
  • -b FILE โ€” FILE exists and is block special
  • -c FILE โ€” FILE exists and is character special
  • -d FILE โ€” FILE exists and is a directory
  • -e FILE โ€” FILE exists
  • -f FILE โ€” FILE exists and is a regular file
  • -g FILE โ€” FILE exists and is set-group-ID
  • -G FILE โ€” FILE exists and is owned by the effective group ID
  • -h FILE โ€” FILE exists and is a symbolic link (same as -L)
  • -k FILE โ€” FILE exists and has its sticky bit set
  • -L FILE โ€” FILE exists and is a symbolic link (same as -h)
  • -N FILE โ€” FILE exists and has been modified since it was last read
  • -O FILE โ€” FILE exists and is owned by the effective user ID
  • -p FILE โ€” FILE exists and is a named pipe
  • -r FILE โ€” FILE exists and read permission is granted
  • -s FILE โ€” FILE exists and has a size greater than zero
  • -S FILE โ€” FILE exists and is a socket
  • -t FD โ€” file descriptor FD is opened on a terminal
  • -u FILE โ€” FILE exists and its set-user-ID bit is set
  • -w FILE โ€” FILE exists and write permission is granted
  • -x FILE โ€” FILE exists and execute (or search) permission is granted

Except for -h and -L, all FILE-related tests dereference (follow) symbolic links. bestie, beware that parentheses need to be escaped (e.g., by backslashes) for shells, or your shell will eat them before test ever sees them ๐Ÿ˜ญ INTEGER may also be -l STRING, which evaluates to the length of STRING.

โš ๏ธ NOTE: Binary -a and -o are inherently ambiguous. Use test EXPR1 && test EXPR2 or test EXPR1 || test EXPR2 instead. seriously. do that. ๐Ÿ’€
โš ๏ธ NOTE: [ honors the --help and --version options, but test does not. test treats each of those as it treats any other nonempty STRING. iconic chaotic behaviour honestly ๐Ÿ™ˆ
โš ๏ธ NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports. ๐Ÿ‘‘

Errors are reported with the expression echoed back and a caret under the argument at fault whenever stderr is a terminal; anything else reading stderr gets the usual single-line message. so if you're piping it somewhere, the fancy little caret moment justโ€ฆ doesn't happen. โœจ


โœจ VERSION โœจ

v(uutils coreutils) 0.12.0


โœจ EXAMPLES โœจ

Test if a given variable is equal to a given string:

test "$MY_VAR" = "/bin/zsh"

Test if a given variable is empty ([z]ero length):

test -z "$GIT_BRANCH"

Test if a [f]ile exists:

test -f "path/to/file"

Test if a [d]irectory does not exist:

test ! -d "path/to/directory"

If A is true, then do B, or C in the case of an error (notice that C may run even if A fails):

test condition && echo "true" || echo "false"

Use test in a conditional statement:

if test -f "path/to/file"; then echo "File exists"; else echo "File does not exist"; fi
๐ŸŒธ The examples are provided by the tldr-pages project <https://tldr.sh> under the CC BY 4.0 License. Please note that, as uutils is a work in progress, some examples might fail.

and that's the whole page bestie, no notes, she ate and left absolutely no crumbs ๐Ÿ’… that was TEST(1) from uutils coreutils 0.12.0, dated 2026-09-25 โ€” go forth and write conditionals that actually work ๐Ÿซถ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from