How To Force The 'true' Command To Return 'false'
2023-07-09 - By Robert Elder
I Published Misinformation
Recently, I published a video containing potentially dangerous misinformation. In this article, I'd like to set the record straight in the hopes of repairing my reputation, and preventing others from making the same mistake that I did.
The misinformation in question can be found in a video that I published a few weeks ago about the 'true' command. In the offending video, I made a statement implying that the 'true' command always returns the value 'true', but this statement is actually false.
To make matters worse, I knew that this statement was false at the time, but I still said it anyway, and for that, I'm sorry.
A Script That Forces '/usr/bin/true' To Return 'False'
To illustrate how the 'true' command can return false, consider the behaviour of the following shell script:
#!/bin/bash
i=0
while /usr/bin/true --version; do
sleep 1;
echo "/usr/bin/true is still returning true."
i=$(($i+1))
if [ $i -ge 3 ]; then
echo "About to close stdout..."
exec >&-
fi
done
1>&2 echo "/usr/bin/true returned false."
This script invokes the 'true' command with the version flag. For the first 3 iterations of this script, the invocation of the 'true' command will return a value that allows the while loop continue. However, after the third iteration, the script executes a special shell syntax that closes standard output. The output from running this script looks like this:
true (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jim Meyering.
/usr/bin/true is still returning true.
true (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jim Meyering.
/usr/bin/true is still returning true.
true (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jim Meyering.
/usr/bin/true is still returning true.
About to close stdout...
/usr/bin/true: write error: Bad file descriptor
/usr/bin/true returned false.
Closing Standard Output
In the script above, this special piece of shell syntax:
>&-
indicates that the standard output file descriptor should be closed. The '>' character is a common symbol that's used to redirect data streams. If the number of the stream is not explicitly specified, it's assumed to have the value '1', which indicates standard output.
In the script above, after the third iteration of the loop, the standard output file descriptor will be closed. This causes the next invocation of the 'true' command to return 'false', causing an exit from the while loop.
You can see this result more clearly by running the 'true' command directly on the command line with the 'help' or 'version' flags, and then checking its return code. With standard output left alone, the return value is zero:
/usr/bin/true --version
echo $?
0
and with standard output closed, the return value is 1:
/usr/bin/true --version >&-
echo $?
1
/usr/bin/true --version 1>&-
echo $?
1
This behaviour is only exhibited by the GNU coreutils version of the 'true' command when using 'help' or 'version' flags while standard output is closed. In the 'true.c' source code, you can see the following comment:
/* Note true(1) will return EXIT_FAILURE in the
edge case where writes fail with GNU specific options. */
Curbing The Spread Of Dangerous Misinformation
This might seem like an irrelevant corner case, but the fact remains: Claiming that the 'true' command always returns true is a false statement. As a society, we need to do better a better job to curb the spread of misinformation like this, which is why it's my duty to write this article.
In fact, I've already lied to you again in this same article, by stating that the 'true' command can sometimes return false. In fact, the 'true' command never returns boolean values like true or false at all. Instead, it uses integer return codes, where the value 0 is commonly used to represent success, a negative integer is used to represent an error code, and a positive integer may represent an error code, or communicate a meaningful result.
Inconsistent Boolean Value Meanings
By convention, in a shell environment, it's common to associate the integer value '0' with the boolean value 'true', and integer value '1' with the boolean value 'false':
However, outside the context of a shell environment, the previous statement is totally false. In traditional boolean logic, the integer value '0' is typically associated with the boolean value 'false', and respectively the integer value '1' is associated with the boolean value 'true':
As you can see, this is the complete opposite truth assignment compared to the one used in a shell environment.
These two different assignments for the boolean values 'true' and 'false' might seem a bit confusing, but all you need to remember is that 'true' command always returns true, except for when it returns false. However, the false command always returns false, and never returns true.
"This is the same set of foundational logical implications that are found in propositional logic." Is what I would have said if such a statement was actually true, but in fact, it's false. In reality, the complete opposite is true.
In propositional logic, true only ever implies true, and it never implies false. On the other hand, while false predictably implies false, false can also imply true:
This is a well known fact among those who are familiar with the construction of rigorous formal mathematical proofs. If you begin your formal proof by including a false premise, you can not only prove what you want to prove, you can literally prove any result you want.
How To Prove That You're The Pope
Legend has it, that famous logician Bertrand Russell used this feature of proofs to formally prove that he is the pope.
As the story goes, Bertrand Russell was conducting a lecture on logic, where he mentioned that "in the sense of material implication, a false proposition implies any proposition".
A student raised his hand and said "In that case, given that 1 = 0, prove that you are the Pope."
Russell immediately replied, "Add 1 to both sides of the equation: then we have 2 = 1. The set containing just me and the Pope has 2 members. But 2 = 1, so it has only 1 member; therefore, I am the Pope."
As you can see, if misinformation is allowed to spread, you can come up with all sorts of absurd conclusions. That's why I wanted to create this helpful article, to simplify the difference between true and false statements.
In Conclusion
The misinformation that I spread in my previous video is something that I'm not proud of, and I promise to do better in the future.
To close off this apology, I just want to finish by saying that 'I didn't do it', even though I did. And even if I did do it, it wasn't my fault. And even if it was my fault, it wasn't that bad. And even if it was that bad, it's none of your business. This experience has taught me a lot about myself, and I feel that I've grown as a person. At this point, I'm looking to move on from my past mistakes, and I look forward to a bright new future where everyone has forgotten about my mistake and no longer reminds me about it.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Intro To 'sha256sum' Command In Linux
Published 2023-08-30 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|