~ Return to the rest of the site

POSIX true(1)

updated 2021-08-18

true(1) is a tool that only quits silently with an exit status of 0. Similarly, false(1) is a tool that only quits silently with an exit status of 1. Recognizing arguments, printing to standard output, reading from standard input, or otherwise exiting with any other status of 0, is a violation of the POSIX specification for true(1). These utilities find use in shell scripting, which, though extremely relevant to these utilities, is beyond the scope of this article.

Because true(1)'s required functionality is so simple a POSIX-compliant implementation is a one-liner in most languages, so long as you're willing to make an exception in your code styling. For example, in C:

int main(void) { return 0; }

Because executing an empty shellscript file will in most shells do nothing and return an exit status of 0, technically an empty shellscript file is a POSIX-compliant true(1) implementation in 0 bytes. This was the true(1) implementation on early versions of UNIX, including Research UNIX, System V, and Sun's Solaris, according to both Rob Pike and John Chambers. A more explicit implementation also exists in POSIX shell:

exit 0

This happens to be nearly identical in source to the implementation used by NetBSD.

Python has the same 0 byte true(1) implementation feature as most shells. Here's false(1) in Python rather than true(1) to demonstrate how exiting with an arbitrary exit status can be done:

import sys

In some shells, true(1) is a shell built-in command, so running true will run the shell author's implementation of true(1) rather than the system implementation.

GNU true(1), from the GNU coreutils, deserves a special mention, as it's eighty lines long and directly includes four C header files. This is not a joke. Their true.c is 2.3 kilobytes, parses the arguments --help and --version (only if either are the first argument to the program), and I don't know how big the executable ends up being because the first thing I do when I take control of a GNU system is dd if=/dev/null of="$(which true)";chmod +x "$(which true)" (use at your own risk). The GNU coreutils implementation of true(1) is not POSIX compliant.

Cited media and further reading