The truth about Linux true and false commands

True and false are common concepts in all forms of computing. After all, they are fundamental to Boolean logic, but did you know that certain Y fake are also the commands in linux? Do you know how to use them?

The simplest explanation is that the certain command generates an exit code of 0 and that the fake The command generates an exit code of 1. However, this explanation does not provide much detail on how these commands can best be used.

In this post, we will see how the certain Y fake the commands work and how you can use them on the command line or in your scripts.

Viewing Exit Codes

First, it is important to remember that a successful exit code (also known as a “return code”) on Linux systems is 0. Think of this as “zero errors”. Exit codes that indicate some type of failure will have values ​​of 1 or more.

Every time you run a command on Linux, an exit code is generated. While you will see the expected output or error messages, you will only see the exit codes if you ask for them. To ask, you just need to use the command eco $?. The $ string represents the exit code and the echo command will display the code like this:

$ echo hello
$ echo $?

The command eco hello succeeded, so the exit code is 0.

Here is a simple example of displaying an exit code when a command is unsuccessful. 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 after and you will see this:

$ echo $?

An exit code of 127 indicates that the command you just typed does not exist on the system.

Here’s another example where we try to display a file that doesn’t exist:

$ cat dhksdfhjksfjhskfhjd
cat: dhksdfhjksfjhskfhjd: No such file or directory
$ echo $?

Exit code 1 is something of a general exit code and will return for a variety of errors.

Using true and false

The easiest way to see how the certain Y fake The commands work on the command line is to run these commands:

$ true; echo $?
$ false; echo $?

As you can see, certain it just returns a 0 and fake it just returns a 1.

Probably the most common use of the certain The command is to start an infinite loop. Instead of starting a loop with a command like While [ $num -le 12345678 ], you can use while it’s true and the loop continues until you stop it, it will be a ^ c.

while true
    echo “still running”
    sleep 10

TO while it is false loop would fail immediately. However, another way to start an infinite loop is to use a syntax like this:

until false; do
    echo still running
    sleep 10

When you stop an infinite loop by typing ^ c and then check the return code, you should see this:

$ run-forever
still running
still running
^C$ echo $?

Exit code 130 confirms that the cycle was terminated with a ^ c.

$ more run-forever
until false; do
    echo still running
    sleep 10

If you want a command to generate a successful exit code even if the command itself fails, you can pipe its output to certain like this:

$ cat nosuchfile | true; echo $?
cat: nosuchfile: No such file or directory
0               <== exit code

You keep getting an error message, but the exit code will be 0 (success).

If you use a Yes test like below, if true it will always execute the specified command and, as you would expect, if it is false it 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 it is false The command has no output because the echo the command is not executed.

Use a colon instead of certain has the same effect as using certain. Here is an example:

$ if :
> then
>     echo This command always runs
> else
>     echo This command never runs
> fi
This command always runs

It is also possible to configure your own exit codes. For example, if you have the command exit 111 In a script as shown in the example below, the script exit code will be 111.


if [ $# == 0 ]; then
    echo “$0 filename”
    exit 1

echo $0
exit 111

When we run the script, we will see something like this:

$ myscript oops

We see the full name of the file that is being verified.

However, when we check the exit code, we will all see this:

$ echo $?


The certain Y fake Commands have limited functionality, but they can be useful when you need some control over exit codes or want to run a command until you are ready to remove it.

Join Network World communities at Facebook Y LinkedIn to comment on the topics that are most important.

Copyright © 2022 IDG Communications, Inc.

Leave a Comment