From e5cf62736ce5fd6aede41481938b4ed4bb648ee8 Mon Sep 17 00:00:00 2001
From: Deven Blake
~ Return to the rest of the site
+ + + +
+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.
+However (TODO) it's to me unknown whether this is implementation-specific or POSIX-specified.
+The usual implementation in POSIX shell is also a one-liner if you ignore the shebang:
+
+#!/bin/sh
+exit 0
+
++This happens to be nearly identical in source to the implementation used by NetBSD. +
+
+Here's false(1)
in Python rather than true(1)
to demonstrate how exiting with an arbitrary exit status can be done:
+
+import sys
+sys.exit(1)
+
+
+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 printf "#/bin/sh\nexit 0\n"|dd of="$(which true)";chmod +x "$(which true)"
(use at your own risk).
+