Boolean values: Difference between revisions

m
m (→‎esoteric: corrected reference name. -- ~~~~)
Line 1,021:
=={{header|UNIX Shell}}==
 
The traditional Bourne shell does not provide a reserved keyword for true or false.
The values ''true'' and ''false'' are defined, respectively, as a return code of 0 and a return code of greater-than zero. While there are built-in functions for each of these values, booleans are most commonly the result of a test or a process termination.
 
Truth is determined by exit codes rather than values
 
The evaluation of true and false within the shell is different to the evaluation of truth from within a high level language. Within the shell, a truth is based on the exitcode of the last command in the evaluation block:
 
* An exitcode of zero is considered to be a true condition
* An exitcode of nonzero is considered to be a false condition
 
In the following example, after running the test command, the then syntactical component runs the optional branch if an exitcode is of zero determined:
 
<lang sh>if
echo 'Looking for file' # This is the evaluation block
test -e foobar.fil # The exit code from this statement determines whether the branch runs
then
echo 'The file exists' # This is the optional branch
echo 'I am going to delete it'
rm foobar.fil
fi</lang>
 
TheIn some later shells, the values ''true'' and ''false'' are defined, respectively, as a return code of 0 and a return code of greater-than zero. While there are built-in functions for each of these values, booleans are most commonly the result of a test or a process termination.
 
<lang Bash>true && echo "true" || echo "false"</lang>