Shell builtins · bash builtin
true
Do nothing and exit with status 0 (success).
true
true does nothing, successfully: it exits with status 0 and prints nothing. That is useful
wherever the shell wants a command but you have nothing to run: an endless while true loop,
a placeholder in an if branch, or || true after a command whose failure you have decided
to tolerate under set -e.
In bash it is a builtin, so it costs no process. There is also an executable,
/usr/bin/true, used when no shell is involved (for example as a do-nothing command in a
config file). The colon command : is the older built-in equivalent and behaves the same,
except that : is a POSIX special builtin.
Its opposite is false.
Examples
Check its exit status
true; echo "exit: $?"
exit: 0
Nothing is printed by true itself; $? shows the status 0.
Loop until something breaks out
n=0
while true; do
n=$((n + 1))
[ "$n" -ge 3 ] && break
done
echo "stopped after $n rounds"
stopped after 3 rounds
while true runs forever on its own; the break inside the loop is the real exit
condition. Common for retry and polling loops.
Tolerate one failing command under set -e
set -e
rm old-cache.tmp 2>/dev/null || true
echo "still running"
still running
Without || true, the failed rm (there is no such file) would end the script immediately
because of set -e. With it, the list as a whole succeeds.
See what happens without || true
set -e
rm old-cache.tmp 2>/dev/null
echo "never printed"
(no output)
The script stops at the failing rm, with rm's status 1, and the echo never runs.
Use the colon builtin the same way
: ; echo "exit: $?"
while :; do echo "once"; break; done
exit: 0 once
: also does nothing and returns 0. You will see it in older scripts and in idioms such as
: "${VAR:=default}", where its arguments are evaluated for their side effects.
Fill an empty branch
if grep -q ERROR logs/app.log; then
true # TODO: alert someone
else
echo "no errors"
fi
echo "exit: $?"
exit: 0
Bash refuses an empty then branch with a syntax error, so a placeholder command is needed.
grep found errors, so the branch ran true and the if statement's status is 0.
See the builtin and the program
type -a true
true is a shell builtin true is /usr/bin/true true is /bin/true
type -a lists every form bash could run: the builtin first (which wins), then the
executables in PATH. /bin is a link to /usr/bin on current Debian, so the program
appears twice.
Know that the builtin ignores --help
true --help; echo "builtin exit: $?"
/usr/bin/true --version | head -1
builtin exit: 0 true (GNU coreutils) 9.7
The bash builtin ignores every argument. GNU /usr/bin/true is the exception to "true does
nothing": it recognizes --help and --version when they are its only argument.
Common pitfalls
Sprinkling
|| trueover a whole script to silenceset -e. It also hides the failures you care about; apply it only to commands whose failure is genuinely acceptable, or test the status explicitly.Writing
while [ true ]orif [ false ]. That tests whether the stringtrueorfalseis non-empty, which is always the case, so[ false ]succeeds. Call the command:while true.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
Every shell has
trueas a builtin, and every system has/usr/bin/trueor/bin/true. Only GNU's executable responds to--help/--version.
Options worth knowing
A selection, described in our own words. For the full list, see true(1).
| Option | What it does |
|---|---|
--help | GNU /usr/bin/true only: print usage and exit. |
--version | GNU /usr/bin/true only: print the version and exit. |
Related
falseDo nothing and exit with status 1 (failure).yesPrint a line (y by default) over and over until killed or the reader stops reading.testEvaluate a condition about files, strings or integers and return 0 for true or 1 for false.setTurn shell options such as -e, -u, -x and pipefail on or off, and replace the positional parameters.typeShow how the shell would interpret a name, as an alias, keyword, function, builtin or file on PATH.- Exit codesWhat exit statuses mean, how $? works, the reserved values 126, 127 and 128+N, PIPESTATUS, and how to choose codes for your own scripts.
- Strict mode and its caveatsWhat set -euo pipefail actually does, the cases where -e silently does nothing, the traps in -u and pipefail, and a balanced way to use it.
- Loopsfor over lists, files and ranges, C-style for, while and until, break and continue, retry loops, and redirecting a whole loop's input or output.