TEST(1B)

πŸ“¦ version 070715, in nixos 26.05, nixpkgs unstable

🌸 test (1B) β€” the BSD condition command, bestie edition πŸ’…

ok so real talk: this page is /ucb/test, the BSD flavor, and it's literally the vibe check of the shell world. It looks at an expression and tells you if it's true or not. That's it. That's the whole job. Iconic, honestly. πŸŽ€


✨ SYNOPSIS ✨

/ucb/test [expr]

πŸ’– DESCRIPTION πŸ’–

So test evaluates the expression expr, and here's the tea: if the expression's value is true, it bounces back with a zero exit status. If it's not true, you get a non zero exit status. 🫢

And bestie, plot twist β€” test also returns a non zero exit if there are no arguments at all. Like, it needs something to work with, okay? 😭

The following primitives are the little building blocks you use to construct expr:

πŸ¦‹ File primaries πŸ¦‹

  • -r file β€” true if the file exists and is readable (you've got read permission, slay).
  • -w file β€” true if the file exists and is writable (go off, write to it).
  • -u file β€” true if the file exists and has the setuid bit set πŸ‘‘
  • -g file β€” true if the file exists and has the setgid bit set πŸ‘‘
  • -k file β€” true if the file exists and has the sticky bit set πŸ“
  • -f file β€” true if the file exists and is not a directory.
  • -d file β€” true if the file exists and is a directory πŸ“
  • -h file β€” true if the file exists and is a symbolic link πŸ”—
  • -L file β€” true if the file exists and is a symbolic link πŸ”— (yes, same energy as -h, both of them are valid, no cap)
  • -p file β€” true if the file exists and is a named pipe πŸ₯€
  • -b file β€” true if the file exists and is a block device πŸ’€
  • -c file β€” true if the file exists and is a character device πŸ’€
  • -s file β€” true if the file exists and has a size greater than zero (it's got substance, we love that)

🧚 Terminal primary 🧚

  • -t [fildes] β€” true if the open file whose file descriptor number is fildes β€” and yes, fildes is 1 by default β€” is associated with a terminal device. So like, are we talking to a real terminal or not? πŸ‘€

🌈 String primaries 🌈

  • -z s1 β€” true if the length of string s1 is zero (empty string detected, nothing there πŸ«₯).
  • -n s1 β€” true if the length of the string s1 is nonzero (there's content in there, periodt).
  • s1 = s2 β€” true if the strings s1 and s2 are equal πŸ’–
  • s1 != s2 β€” true if the strings s1 and s2 are not equal πŸ™…
  • s1 β€” true if s1 is not the null string (just the string by itself, standing there, being valid).

⭐ Integer primary ⭐

  • n1 -eq n2 β€” true if the integers n1 and n2 are algebraically equal. And get this: any of the comparisons -ne, -gt, -ge, -lt, or -le may be used in place of -eq. Mix and match, bestie, the whole squad's here πŸ’…

πŸ”₯ OPERATORS πŸ”₯

These primaries may be combined with the following operators:

  • ! β€” unary negation operator (the "actually, no" of the group chat) πŸ™ˆ
  • -a β€” binary and operator πŸ’œ
  • -o β€” binary or operator πŸ’œ
  • ( expr ) β€” parentheses for grouping πŸŽ€
⚠️ ok important warning bestie: -a has higher precedence than -o. So keep that in mind when you're stacking things up, or you'll get a result you did not manifest, fr fr. 😭

Also β€” huge one β€” notice that all the operators and flags are separate arguments to test. Like, they're not glued onto each other, each one is its own argument. And notice also that parentheses are meaningful as command separators and must be escaped. So if you just type them raw in a shell, they're gonna do shell things instead of test things. Escape them, bestie, please, for me πŸ™


🫢 SEE ALSO 🫢

Not gonna lie, this page doesn't list any SEE ALSO references β€” that's just how it is, no cap. The whole thing is self-contained, main character energy only πŸ‘‘


okayyy that's the whole page, go forth and vibe check your file system πŸ’…βœ¨

this lil gem comes from the Heirloom Toolchest, dated 7/2/05 β€” shoutout to the BSD System Compatibility crew for keeping it iconic πŸŽ€

πŸ“¦ where this page comes from