How the true and false commands work, and how to put them to use on the command line and in scripts. Credit: sarawuth702 / Getty Images True and false are common concepts in all forms of computing. They’re critical to Boolean logic after all, but did you know that true and false are also commands on Linux? Do you know how to use them? The simplest explanation is that the true command generates an exit code of 0 and that the false command generates an exit code of 1. This explanation, however, doesn’t provide much detail on how these commands can best be used. In this post, we’ll look at how the true and false commands work and how you might put them to use on the command line or in your scripts. Viewing exit codes First, it’s important to remember that a successful exit code (a.k.a. “return code”) on Linux systems is 0. Think of this as meaning “zero errors”. Exit codes indicating some form of failure will have values of 1 or greater. Anytime you run a command on Linux, an exit code is generated. While you’ll see the expected output or error messages, you’ll only see the exit codes if you ask. To ask, you just need to use the command echo $?. The $? string represents the exit code and the echo command will display the code like this: $ echo hello hello $ echo $? 0 The command echo hello was successful, so the exit code is 0. Here’s a simple example of displaying an exit code when a command is not successful. Hit a bunch of random keys on your keyboard, and you’ll end up seeing something like this: $ asjdlkjdad bash: asjdlkjdad: command not found... Ask to see the exit code immediately afterwards and you’ll see this: $ echo $? 127 An exit code of 127 indicates that the command you just typed doesn’t exist on the system. Here’s another example in which we try to display a file that doesn’t exist: $ cat dhksdfhjksfjhskfhjd cat: dhksdfhjksfjhskfhjd: No such file or directory $ echo $? 1 The 1 exit code is something of a general exit code and will be returned for a variety of errors. Using true and false The simplest way to see how the true and false commands work on the command line is to run these commands: $ true; echo $? 0 $ false; echo $? 1 As you can see, true simply returns a 0 and false simply returns a 1. Probably the most common use of the true command is to start an infinite loop. Instead of starting a loop with a command like while [ $num -le 12345678 ], you can use while true and the loop continues until you stop it will a ^c. while true do echo “still running” sleep 10 done A while false loop would fail immediately. However, another way to start an infinite loop is to use syntax like this: until false; do echo still running sleep 10 done When you halt an otherwise infinite loop by typing ^c and then check the return code, you should see this: $ run-forever still running still running ^C$ echo $? 130 The 130 exit code confirms that the loop was terminated with a ^c. $ more run-forever until false; do echo still running sleep 10 done If you want a command to result in a successful exit code even if the command itself fails, you can pipe its output to true like this: $ cat nosuchfile | true; echo $? cat: nosuchfile: No such file or directory 0 You still get an error message, but the exit code will be 0 (success). If you use an if test like those shown below, if true will always run the command specified and, as you likely expect, if false will never execute the embedded command. if true > then > echo This command always runs > fi This command always runs if false > then > echo This command never runs > fi $ As you see, the if false command has no output because the echo command isn’t run. Using a colon in place of true has the same effect as using true. Here’s an example: $ if : > then > echo This command always runs > else > echo This command never runs > fi This command always runs It’s also possible to set your own exit codes. For example, if you have the command exit 111 in a script as shown in the example below, the exit code for the script will be 111. #!/bin/bash if [ $# == 0 ]; then echo “$0 filename” exit 1 fi echo $0 exit 111 When we run the script, we’ll see something like this: $ myscript oops /home/shs/bin/oops We see the full name of the file being verified. When we check the exit code, however, all we’ll see this: $ echo $? 111 Wrap-Up The true and false commands have limited functionality, but they can be helpful when you need some control over exit codes or you want to run a command until you’re ready to kill it. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe