TEST(1plan9)

πŸ’– NAME πŸ’–

test β€” sets the exit status according to whether a condition is true or not, no cap 🫢


✨ SYNOPSIS ✨

test expr

πŸŽ€ DESCRIPTION πŸŽ€

Okay so test evaluates the expression expr. If that value comes out true, the exit status is null (that's the good one, zero, the slay). Otherwise the exit status is non-null πŸ’…. And listen β€” if you pass it no arguments at all, the exit status is non-null. Empty-handed? Not the vibe. 😭

These are the primitives you build expr out of, bestie:

  • -r file β€” true if the file exists (like, is accessible) and you can read it πŸ“–
  • -w file β€” true if the file exists and you can write to it ✍️
  • -x file β€” true if the file exists and it has execute permission πŸƒ
  • -e file β€” true if the file exists, periodt
  • -f file β€” true if the file exists and it's a plain file
  • -d file β€” true if the file exists and it's a directory πŸ“
  • -s file β€” true if the file exists and its size is greater than zero
  • -t fildes β€” true if the open file whose file descriptor number is fildes (1 by default, bestie, that's stdout energy) is the same file as /dev/cons
  • -A file β€” true if the file exists and it's append-only
  • -L file β€” true if the file exists and it's exclusive-use
  • -Tfile β€” true if the file exists and it's temporary
  • s1 = s2 β€” true if the strings s1 and s2 are identical πŸ‘―
  • s1 != s2 β€” true if the strings s1 and s2 are not identical πŸ’”
  • s1 β€” true if s1 is not the null string. Deprecated, so like… it's giving vintage, but pls move on 🫠
  • -n s1 β€” true if the length of string s1 is non-zero
  • -z s1 β€” true if the length of string s1 is zero
  • n1 -eq n2 β€” true if the integers n1 and n2 are arithmetically equal. Any of the comparisons -ne, -gt, -ge, -lt, or -le can slide in where -eq was. And the (nonstandard, iykyk) construct -l string β€” meaning the length of string β€” can be used in place of an integer ✨
  • a -nt b β€” true if file a is newer than (modified after) file b ⏰
  • a -ot b β€” true if file a is older than (modified before) file b πŸ•°οΈ
  • f -older t β€” true if file f is older than (modified before) time t. If t is an integer followed by the letters y (years), M (months), d (days), h (hours), m (minutes), or s (seconds), it represents current time minus the specified time. If there's no letter, it represents seconds since epoch. You can also concatenate mixed units, obsessed 😌. For example, 3d12h means three days and twelve hours ago.

🌈 Operators, the combiner era 🌈

You can combine those primaries with these operators:

  • ! β€” unary negation operator, the "actually no" πŸ’€
  • -o β€” binary or operator
  • -a β€” binary and operator; it has higher precedence than -o πŸ‘‘
  • ( expr ) β€” parentheses for grouping
Lowkey important: the primitives -b, -u, -g, and -s return false. They're only recognized for compatibility with POSIX. So if you were expecting them to do something cute β€” they're just standing there. 🧍

Notice that all the operators and flags are separate arguments to test. Notice also that parentheses and equal signs are meaningful to rc and must be enclosed in quotes. Yes fr fr, quote them. 🫢


πŸ¦‹ EXAMPLES πŸ¦‹

Okay bestie, real talk: test is a dubious way to check for specific character strings β€” it spins up a whole process to do what an rc(1) match or switch statement can do with zero effort. The first example is not only inefficient but wrong, because test reads the purported string "-c" as an option πŸ˜­πŸ’€:

if (test $1 '=' "-c") echo OK # wrong!

A better way is:

if (~ $1 -c) echo OK

Test whether abc is in the current directory:

test -f abc -o -d abc

πŸ’œ SOURCE πŸ’œ

/src/cmd/test.c


⭐ SEE ALSO ⭐

rc(1)


That's the whole page, bestie β€” this was TEST(1plan9), straight outta the 1plan9 manual, signed off in true TEST(1plan9) fashion. Go forth and exit status zero, my love πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from