Understanding Linux's True and False Commands
True and False are the most common concepts in all forms of computing. They’re so highly critical to Boolean logic, but did you know that true and false are also even commands on Linux?
There is a simple explanation. This 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.
First, it’s important to remember that a successful exit code – also known as the return code – on Linux systems is 0. You can think of this as meaning zero errors. Exit codes indicating some form of failure will usually 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:
$ vpsnode
bash: coderoasis: command not found...
An exit code of 127 indicates that the command you just typed doesn’t exist on the system.
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 <== exit code
You still get an error message, but the exit code will be 0 (success).
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/vpsnode/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
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.
Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Implementing RSA in Python from Scratch as your next choice.
The CoderOasis Community
Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!
Comments ()