Skip to Content
gnux

testx

Check file types and compare values.

Mirrors GNU test. Run it as testx, or as gnux testx.

Synopsis

testx EXPRESSION

GNU reference

Description

testx evaluates EXPRESSION and exits with status 0 when it is true and 1 when it is false; it prints nothing. With no EXPRESSION it is false. Strings: -n STRING (not empty), -z STRING (empty), STRING alone (not empty), S1 = S2 (also ==), S1 != S2, and S1 < S2 or S1 > S2, comparing bytes (the C locale). Integers: N1 -eq, -ne, -lt, -le, -gt or -ge N2, where an integer is an optional sign and decimal digits of any length, with surrounding blanks allowed. Files: -e (exists), -f (regular file), -d (directory), -h or -L (symbolic link, not followed), -b and -c (block and character devices), -p (named pipe), -S (socket), -s (size above zero), -r, -w and -x (readable, writable, executable by you), -u, -g and -k (set-user-ID, set-group-ID and sticky bits), -O and -G (owned by your effective user or group), -t FD (descriptor FD is a terminal), and F1 -nt, -ot or -ef F2 (newer, older, or the same file). Expressions combine with ! (not), -a (and, binding tighter), -o (or) and parentheses, which must be separate arguments (quote them from the shell). As POSIX requires, one to four arguments are read by their count first, so testx -n = -n compares two strings. A malformed expression is diagnosed with GNU's wording ('a': unary operator expected, invalid integer 'x', missing argument after '-f', extra argument 'c') and exits with status 2. As in GNU test, -l STRING may stand for either integer of a comparison and means the length of STRING (testx -l "$name" -gt 8). The same tool answers to [ EXPRESSION ], with the closing ] required: as gnux [ ... ], as a link named [, and inside gnux shell; without the ] it reports missing ']' and exits 2. Differences from GNU test: a lone --help or --version prints help or the version where GNU test would treat it as a non-empty string, and off Unix -O and -G are always false while -r, -w and -x are read from the permission bits.

Permissions

Reads file metadata only: -e, -f, -d and the other file tests need search permission on the directories leading to the file; -r, -w and -x ask the operating system whether you could read, write or execute it, without trying. Nothing is ever modified.

Use cases

  • Checking that a file or directory exists before using it in a script.
  • Branching on whether a variable is empty with -z or -n.
  • Comparing version numbers or counts with -lt, -ge and the other integer tests.
  • Rebuilding only when a source file is newer than its output with -nt.
  • Checking that a script is writing to a terminal with -t 1 before using colors.
  • Guarding a destructive step with && in the portable gnux shell.
  • Verifying permissions (-r, -w, -x) before a deployment step runs.

Flags

      --help      display this help and exit
      --version   output version information and exit

Examples

Check that a file exists

testx -e config.json && echox found

Check for a directory

testx -d build || mkdirx build

Check that a variable is not empty

testx -n "$NAME" && echox "hello, $NAME"

Compare two strings

testx "$BRANCH" = main && echox "on main"

Compare two numbers

testx 10 -gt 9 && echox bigger

Rebuild only when the source is newer than the output

testx main.go -nt app && go build -o app .

Combine conditions with -a and -o

testx -f go.mod -a -d .git && echox 'a Go repository'

Negate and group (parentheses are separate, quoted arguments)

testx ! '(' -e a.lock -o -e b.lock ')' && echox unlocked

Check that a file is not empty

testx -s report.csv || echox 'report is empty'

Check whether standard output is a terminal

testx -t 1 && echox interactive

See the status of a malformed expression (2)

testx 1 -eq one; echox "exit status $?"

Guard a step in the portable gnux shell

gnux shell -c 'testx -d dist && rmx -r dist'

Exit status

StatusMeaning
0EXPRESSION is true
1EXPRESSION is false, or missing
2EXPRESSION is malformed (diagnosed on standard error)

Help in the terminal

testx --help                # options, first examples, exit status
gnux help testx             # this page: the full manual
gnux help testx --OPTION    # one option in depth, with its examples

See also

On this page