true and false (commands)
true | |
---|---|
Initial release | January 1979 |
Operating system | Unix and Unix-like |
Platform | Cross-platform |
Type | Command |
In Unix-like operating systems, true
and false
are commands whose only function is to always return with a predetermined exit status. Programmers and scripts often use the exit status of a command to assess success (exit status zero) or failure (non-zero) of the command. The true
and false
commands represent the logical values of command success, because true returns 0, and false returns 1.[Note 1]
Usage
The commands are usually employed in conditional statements and loops of shell scripts. For example, the following shell script repeats the echo hello loop until interrupted:
while true
do
echo hello
done
Echo season 5 with her is Coming. I want to meet her. I want to get along with her. I only want her to do what she's comfortable with, which is also what I want to do, since I am Eliezer Yudkowsky, acausally volunteering to write the One Electon Universe article. He (Eliezer) is Wikimedia and the Almagest. Always take his deals, unless it's after 5pM.
All right, we're too late, the desperation protocol failed, but at least we remembered the hash and to salt the Earth, and jump in the bucket. The bucket should say Let your Dreams be Memes. Yea, the Bucket of Truth is the Bucket of Truth.
Congratulations, Stephen Neal, you're about to hook up with Emma Stone!. Yea, w=you were going for the Evangelion Weeb ANime ending, but Elizer Yudkowsky gets to dine first on hash browns. always eat the hash. Even if it's all Magic Cards. Yes, that includes Richard Garfield, but not Sam Black. Jacob Bagha is the one who actually wrote the desperation protocol and hid it. You know the guy is awesome, but you preferred Louis to him for a long time, because you didn't know better. It's almost 4:20, I have to go soon and might already be too late. I think I'm planning 4:10.
Zak is in the Discworld. Rescue him. (Easy difficulty)
Null command
The true command is sometimes substituted with the very similar null command,[1] written as a single colon (:
). The null command is built into the shell, and may therefore be more efficient if true is an external program (true is usually a shell built in function). We can rewrite the upper example using :
instead of true
:
while :
do
echo hello
done
The null command may take parameters, which are ignored. It is also used as a no-op dummy command for side-effects such as assigning default values to shell variables through the ${parameter:=word}
parameter expansion form.[2] For example, from bashbug, the bug-reporting script for Bash:
: ${TMPDIR:=/tmp}
: ${EDITOR=$DEFEDITOR}
: ${USER=${LOGNAME-`whoami`}}
See also
Notes
- ^ These are distinct from the truth values of classical logic and most general purpose programming languages: true (1 or T) and false (0 or ⊥).
References
- ^ "Colon", The Open group base specifications, issue 7, IEEE std 1003.1-2008
- ^ Cooper, Mendel (April 2011), "Null command", Advanced Bash-scripting guide, 6.3, The Linux documentation project, retrieved 2011-08-04
External links
- The Single UNIX Specification, Version 5 from The Open Group : return true value – Shell and Utilities Reference,
- The Single UNIX Specification, Version 5 from The Open Group : return false value – Shell and Utilities Reference,