Booleans in shell script [duplicate] - bash
I tried to declare a Boolean variable in a shell script using the following syntax:
variable=$false
variable=$true
Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using Boolean variables as expressions correct?
if [ $variable ]
if [ !$variable ]
Revised Answer (Feb 12, 2014)
the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
echo 'Be careful not to fall off!'
fi
Original Answer
Caveats: https://stackoverflow.com/a/21210966/89391
the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
echo 'Be careful not to fall off!'
fi
From: Using boolean variables in Bash
The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson's comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.
TL;DR
my_bool=true
if [ "$my_bool" = true ]
Issues with Miku's (original) answer
I do not recommend the accepted answer1. Its syntax is pretty, but it has some flaws.
Say we have the following condition.
if $var; then
echo 'Muahahaha!'
fi
In the following cases2, this condition will evaluate to true and execute the nested command.
# Variable var not defined beforehand. Case 1
var='' # Equivalent to var="". # Case 2
var= # Case 3
unset var # Case 4
var='<some valid command>' # Case 5
Typically you only want your condition to evaluate to true when your "Boolean" variable, var in this example, is explicitly set to true. All the other cases are dangerously misleading!
The last case (#5) is especially naughty because it will execute the command contained in the variable (which is why the condition evaluates to true for valid commands3, 4).
Here is a harmless example:
var='echo this text will be displayed when the condition is evaluated'
if $var; then
echo 'Muahahaha!'
fi
# Outputs:
# this text will be displayed when the condition is evaluated
# Muahahaha!
Quoting your variables is safer, e.g. if "$var"; then. In the above cases, you should get a warning that the command is not found. But we can still do better (see my recommendations at the bottom).
Also see Mike Holt's explanation of Miku's original answer.
Issues with Hbar's answer
This approach also has unexpected behavior.
var=false
if [ $var ]; then
echo "This won't print, var is false!"
fi
# Outputs:
# This won't print, var is false!
You would expect the above condition to evaluate to false, thus never executing the nested statement. Surprise!
Quoting the value ("false"), quoting the variable ("$var"), or using test or [[ instead of [, do not make a difference.
What I do recommend:
Here are ways I recommend you check your "Booleans". They work as expected.
my_bool=true
if [ "$my_bool" = true ]; then
if [ "$my_bool" = "true" ]; then
if [[ "$my_bool" = true ]]; then
if [[ "$my_bool" = "true" ]]; then
if [[ "$my_bool" == true ]]; then
if [[ "$my_bool" == "true" ]]; then
if test "$my_bool" = true; then
if test "$my_bool" = "true"; then
They're all pretty much equivalent. You'll have to type a few more keystrokes than the approaches in the other answers5, but your code will be more defensive.
Footnotes
Miku's answer has since been edited and no longer contains (known) flaws.
Not an exhaustive list.
A valid command in this context means a command that exists. It doesn't matter if the command is used correctly or incorrectly. E.g. man woman would still be considered a valid command, even if no such man page exists.
For invalid (non-existent) commands, Bash will simply complain that the command wasn't found.
If you care about length, the first recommendation is the shortest.
There seems to be some misunderstanding here about the Bash builtin true, and more specifically, about how Bash expands and interprets expressions inside brackets.
The code in miku's answer has absolutely nothing to do with the Bash builtin true, nor /bin/true, nor any other flavor of the true command. In this case, true is nothing more than a simple character string, and no call to the true command/builtin is ever made, neither by the variable assignment, nor by the evaluation of the conditional expression.
The following code is functionally identical to the code in the miku's answer:
the_world_is_flat=yeah
if [ "$the_world_is_flat" = yeah ]; then
echo 'Be careful not to fall off!'
fi
The only difference here is that the four characters being compared are 'y', 'e', 'a', and 'h' instead of 't', 'r', 'u', and 'e'. That's it. There's no attempt made to call a command or builtin named yeah, nor is there (in miku's example) any sort of special handling going on when Bash parses the token true. It's just a string, and a completely arbitrary one at that.
Update (2014-02-19): After following the link in miku's answer, now I see where some of the confusion is coming from. Miku's answer uses single brackets, but the code snippet he links to does not use brackets. It's just:
the_world_is_flat=true
if $the_world_is_flat; then
echo 'Be careful not to fall off!'
fi
Both code snippets will behave the same way, but the brackets completely change what's going on under the hood.
Here's what Bash is doing in each case:
No brackets:
Expand the variable $the_world_is_flat to the string "true".
Attempt to parse the string "true" as a command.
Find and run the true command (either a builtin or /bin/true, depending on the Bash version).
Compare the exit code of the true command (which is always 0) with 0. Recall that in most shells, an exit code of 0 indicates success and anything else indicates failure.
Since the exit code was 0 (success), execute the if statement's then clause
Brackets:
Expand the variable $the_world_is_flat to the string "true".
Parse the now-fully-expanded conditional expression, which is of the form string1 = string2. The = operator is bash's string comparison operator. So...
Do a string comparison on "true" and "true".
Yep, the two strings were the same, so the value of the conditional is true.
Execute the if statement's then clause.
The no-brackets code works, because the true command returns an exit code of 0, which indicates success. The bracketed code works, because the value of $the_world_is_flat is identical to the string literal true on the right side of the =.
Just to drive the point home, consider the following two snippets of code:
This code (if run with root privileges) will reboot your computer:
var=reboot
if $var; then
echo 'Muahahaha! You are going down!'
fi
This code just prints "Nice try." The reboot command is not called.
var=reboot
if [ $var ]; then
echo 'Nice try.'
fi
Update (2014-04-14) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a Bash-specific synonym for =, and as far as I've seen, they work exactly the same in all contexts.
Note, however, that I'm specifically talking about the = and == string comparison operators used in either [ ] or [[ ]] tests. I'm not suggesting that = and == are interchangeable everywhere in bash.
For example, you obviously can't do variable assignment with ==, such as var=="foo" (well technically you can do this, but the value of var will be "=foo", because Bash isn't seeing an == operator here, it's seeing an = (assignment) operator, followed by the literal value ="foo", which just becomes "=foo").
Also, although = and == are interchangeable, you should keep in mind that how those tests work does depend on whether you're using it inside [ ] or [[ ]], and also on whether or not the operands are quoted. You can read more about that in Advanced Bash Scripting Guide: 7.3 Other Comparison Operators (scroll down to the discussion of = and ==).
Use arithmetic expressions.
#!/bin/bash
false=0
true=1
((false)) && echo false
((true)) && echo true
((!false)) && echo not false
((!true)) && echo not true
Output:
true
not false
Long story short:
There are no Booleans in Bash
The true and false commands
Bash does have Boolean expressions in terms of comparison and conditions. That said, what you can declare and compare in Bash are strings and numbers. That's it.
Wherever you see true or false in Bash, it's either a string or a command/builtin which is only used for its exit code.
This syntax...
if true; then ...
is essentially...
if COMMAND; then ...
where the command is true. The condition is true whenever the command returns exit code 0. true and false are Bash builtins and sometimes also standalone programs that do nothing but returning the corresponding exit code.
Conditions in if..then..fi
When using square brackets or the test command, you rely on the exit code of that construct. Keep in mind that [ ] and [[ ]] are also just commands/builtins like any other. So ...
if [[ 1 == 1 ]]; then echo yes; fi
corresponds to
if COMMAND; then echo yes; fi
and the COMMAND here is [[ with the parameters 1 == 1 ]]
The if..then..fi construct is just syntactic sugar. You can always just run the commands separated by a double ampersand for the same effect:
[[ 1 == 1 ]] && echo yes
When using true and false in these testing constructs you are actually only passing the string "true" or "false" to the testing command. Here is an example:
Believe it or not but those conditions are all yielding the same result:
if [[ false ]]; then ...
if [[ "false" ]]; then ...
if [[ true ]]; then ...
if [[ "true" ]]; then ...
TL;DR; always compare against strings or numbers
To make this clear to future readers, I would recommend always using quotes around true and false:
DO
if [[ "${var}" == "true" ]]; then ...
if [[ "${var}" == "false" ]]; then ...
if [[ "${var}" == "yes" ]]; then ...
if [[ "${var}" == "USE_FEATURE_X" ]]; then ...
if [[ -n "${var:-}" ]]; then echo "var is not empty" ...
DON'T
# Always use double square brackets in bash!
if [ ... ]; then ...
# This is not as clear or searchable as -n
if [[ "${var}" ]]; then ...
# Creates impression of Booleans
if [[ "${var}" != true ]]; then ...
# `-eq` is for numbers and doesn't read as easy as `==`
if [[ "${var}" -eq "true" ]]; then ...
Maybe
# Creates impression of Booleans.
# It can be used for strict checking of dangerous operations.
# This condition is false for anything but the literal string "true".
if [[ "${var}" != "true" ]]; then ...
Long ago, when all we had was sh, Booleans where handled by relying on a convention of the test program where test returns a false exit status if run without any arguments.
This allows one to think of a variable that is unset as false and variable set to any value as true. Today, test is a builtin to Bash and is commonly known by its one-character alias [ (or an executable to use in shells lacking it, as dolmen notes):
FLAG="up or <set>"
if [ "$FLAG" ] ; then
echo 'Is true'
else
echo 'Is false'
fi
# Unset FLAG
# also works
FLAG=
if [ "$FLAG" ] ; then
echo 'Continues true'
else
echo 'Turned false'
fi
Because of quoting conventions, script writers prefer to use the compound command [[ that mimics test, but has a nicer syntax: variables with spaces do not need to be quoted; one can use && and || as logical operators with weird precedence, and there are no POSIX limitations on the number of terms.
For example, to determine if FLAG is set and COUNT is a number greater than 1:
FLAG="u p"
COUNT=3
if [[ $FLAG && $COUNT -gt '1' ]] ; then
echo 'Flag up, count bigger than 1'
else
echo 'Nope'
fi
This stuff can get confusing when spaces, zero length strings, and null variables are all needed and also when your script needs to work with several shells.
Instead of faking a Boolean and leaving a trap for future readers, why not just use a better value than true and false?
For example:
build_state=success
if something-horrible; then
build_state=failed
fi
if [[ "$build_state" == success ]]; then
echo go home; you are done
else
echo your head is on fire; run around in circles
fi
How can I declare and use Boolean variables in a shell script?
Unlike many other programming languages, Bash does not segregate its variables by "type." [1]
So the answer is pretty clear. There isn't any Boolean variable in Bash.
However:
Using a declare statement, we can limit the value assignment to
variables.[2]
#!/bin/bash
declare -ir BOOL=(0 1) # Remember BOOL can't be unset till this shell terminates
readonly false=${BOOL[0]}
readonly true=${BOOL[1]}
# Same as declare -ir false=0 true=1
((true)) && echo "True"
((false)) && echo "False"
((!true)) && echo "Not True"
((!false)) && echo "Not false"
The r option in declare and readonly is used to state explicitly that the variables are readonly. I hope the purpose is clear.
My findings and suggestion differ a bit from the other posts. I found that I could use "booleans" basically as one would in any "regular" language, without the "hoop jumping" suggested...
There isn't any need for [] or explicit string comparisons... I tried multiple Linux distributions. I tested Bash, Dash, and BusyBox. The results were always the same. I'm not sure what the original top voted posts are talking about. Maybe times have changed and that's all there is to it?
If you set a variable to true, it subsequently evaluates as an "affirmative" within a conditional. Set it to false, and it evaluates to a "negative". Very straightforward! The only caveat, is that an undefined variable also evaluates like true! It would be nice if it did the opposite (as it would in most languages), but that's the trick - you just need to explicitly initialize your booleans to true or false.
Why does it work this way? That answer is two fold. A) true/false in a shell really means "no error" vs "error" (i.e. 0 vs anything else). B) true/false are not values - but rather statements in shell scripting! Regarding the second point, executing true or false on a line by itself sets the return value for the block you're in to that value, i.e. false is a declaration of "error encountered", where true "clears" that. Using it with an assignment to a variable "returns" that into the variable. An undefined variable evaluates like true in a conditional because that equally represents 0 or "no error encountered".
See the example Bash lines and results below. Test it yourself if you want to confirm...
#!/bin/sh
# Not yet defined...
echo "when set to ${myBool}"
if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi;
myBool=true
echo "when set to ${myBool}"
if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi;
myBool=false
echo "when set to ${myBool}"
if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi;
Yields
when set to
it evaluates to true
when set to true
it evaluates to true
when set to false
it evaluates to false
POSIX (Portable Operating System Interface)
I miss here the key point, which is portability. That's why my header has POSIX in itself.
Essentially, all of the voted answers are correct, with the exception they are Bash-specific too much.
Basically, I only wish to add more information about portability.
[ and ] brackets like in [ "$var" = true ] are not necessary, and you can omit them and use the test command directly:
test "$var" = true && yourCodeIfTrue || yourCodeIfFalse
Important note: I no longer recommend this as it's being slowly deprecated and more difficult to combine multiple statements.
Imagine what those words true and false mean to the shell, test it yourself:
echo $(( true ))
0
echo $(( false ))
1
But using quotes:
echo $(( "true" ))
bash: "true": syntax error: operand expected (error token is ""true"")
sh (dash): sh: 1: arithmetic expression: expecting primary: ""true""
The same goes for:
echo $(( "false" ))
The shell can't interpret it other than a string. I hope you are getting the idea of how good it is using proper keyword without quotes.
But no one said it in previous answers.
What does this mean? Well, several things.
You should get used to the Boolean keywords are actually treated like numbers, that is true = 0 and false = 1, remember all non-zero values are treated like false.
Since they are treated as numbers, you should treat them like that too, i.e. if you define variable say:
var_bool=true
echo "$var_bool"
true
you can create an opposite value of it with:
var_bool=$(( 1 - $var_bool )) # same as $(( ! $var_bool ))
echo "$var_bool"
1
As you can see for yourself, the shell does print true string for the first time you use it, but since then, it all works via number 0 representing trueor 1 representing false, respectively.
Finally, what you should do with all that information
First, one good habit would be assigning 0 instead of true; 1 instead of false.
Second good habit would be to test if the variable is / isn't equal to zero:
if [ "$var_bool" -eq 0 ]; then
yourCodeIfTrue
else
yourCodeIfFalse
fi
In many programming languages, the Boolean type is, or is implemented as, a subtype of integer, where true behaves like 1 and false behaves like 0:
Boolean in C
Boolean in Python
Boolean in Java
Mathematically, Boolean algebra resembles integer arithmetic modulo 2. Therefore, if a language doesn't provide native Boolean type, the most natural and efficient solution is to use integers. This works with almost any language. For example, in Bash you can do:
# val=1; ((val)) && echo "true" || echo "false"
true
# val=0; ((val)) && echo "true" || echo "false"
false
man bash:
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
Regarding syntax, this is a simple methodology that I use (by example) to consistently and sanely manage Boolean logic:
# Tests
var=
var=''
var=""
var=0
var=1
var="abc"
var=abc
if [[ -n "${var}" ]] ; then
echo 'true'
fi
if [[ -z "${var}" ]] ; then
echo 'false'
fi
# Results
# var= # false
# var='' # false
# var="" # false
# var=0 # true
# var=1 # true
# var="abc" # true
# var=abc # true
If the variable is never declared the answer is: # false
So, a simple way to set a variable to true (using this syntax methodology) would be, var=1; conversely, var=''.
Reference:
-n = True if the length of var string is non-zero.
-z = True if the length of var string is zero.
Bill Parker is getting voted down, because his definitions are reversed from the normal code convention. Normally, true is defined as 0 and false is defined as nonzero. 1 will work for false, as will 9999 and -1. The same with function return values - 0 is success and anything nonzero is failure. Sorry, I don't have the street credibility yet to vote or to reply to him directly.
Bash recommends using double brackets now as a habit instead of single brackets, and the link Mike Holt gave explains the differences in how they work. 7.3. Other Comparison Operators
For one thing, -eq is a numerical operator, so having the code
#**** NOTE *** This gives error message *****
The_world_is_flat=0;
if [ "${The_world_is_flat}" -eq true ]; then
will issue an error statement, expecting an integer expression. This applies to either parameter, as neither is an integer value. Yet, if we put double brackets around it, it will not issue an error statement, but it will yield a wrong value (well, in 50% of the possible permutations). It will evaluate to [[0 -eq true]] = success, but also to [[0 -eq false]] = success, which is wrong (hmmm.... what about that builtin being a numerical value?).
#**** NOTE *** This gives wrong output *****
The_world_is_flat=true;
if [[ "${The_world_is_flat}" -eq true ]]; then
There are other permutations of the conditional which will give wrong output as well. Basically, anything (other than the error condition listed above) that sets a variable to a numerical value and compares it to a true/false builtin, or sets a variable to a true/false builtin and compares it to a numerical value. Also, anything that sets a variable to a true/false builtin and does a comparison using -eq. So avoid -eq for Boolean comparisons and avoid using numerical values for Boolean comparisons. Here's a summary of the permutations that will give invalid results:
# With variable set as an integer and evaluating to true/false
# *** This will issue error warning and not run: *****
The_world_is_flat=0;
if [ "${The_world_is_flat}" -eq true ]; then
# With variable set as an integer and evaluating to true/false
# *** These statements will not evaluate properly: *****
The_world_is_flat=0;
if [ "${The_world_is_flat}" -eq true ]; then
#
if [[ "${The_world_is_flat}" -eq true ]]; then
#
if [ "${The_world_is_flat}" = true ]; then
#
if [[ "${The_world_is_flat}" = true ]]; then
#
if [ "${The_world_is_flat}" == true ]; then
#
if [[ "${The_world_is_flat}" == true ]]; then
# With variable set as an true/false builtin and evaluating to true/false
# *** These statements will not evaluate properly: *****
The_world_is_flat=true;
if [[ "${The_world_is_flat}" -eq true ]]; then
#
if [ "${The_world_is_flat}" = 0 ]; then
#
if [[ "${The_world_is_flat}" = 0 ]]; then
#
if [ "${The_world_is_flat}" == 0 ]; then
#
if [[ "${The_world_is_flat}" == 0 ]]; then
So, now to what works. Use true/false builtins for both your comparison and your evaluations (as Mike Hunt noted, don't enclose them in quotes). Then use either or single or double equal sign (= or ==) and either single or double brackets ([ ] or [[ ]]). Personally, I like the double equals sign, because it reminds me of logical comparisons in other programming languages, and double quotes just because I like typing. So these work:
# With variable set as an integer and evaluating to true/false
# *** These statements will work properly: *****
#
The_world_is_flat=true/false;
if [ "${The_world_is_flat}" = true ]; then
#
if [[ "${The_world_is_flat}" = true ]]; then
#
if [ "${The_world_is_flat}" = true ]; then
#
if [[ "${The_world_is_flat}" == true ]]; then
There you have it.
My receipe to (my own) idiocy:
# setting ----------------
commonMode=false
if [[ $something == 'COMMON' ]]; then
commonMode=true
fi
# using ----------------
if $commonMode; then
echo 'YES, Common Mode'
else
echo 'NO, no Common Mode'
fi
$commonMode && echo 'commonMode is ON ++++++'
$commonMode || echo 'commonMode is OFF xxxxxx'
Another way of using booleans is to test the emptyness of values. This has the advantage of making shorter tests:
first=1 # A true value
second= # A false value
[ -n "$first" ] && echo 'First var is true'
[ -z "$first" ] && echo 'First var is false'
[ -n "$second" ] && echo 'Second var is true'
[ -z "$second" ] && echo 'Second var is false'
Output:
First var is true
Second var is false
Here is an alternative test syntax with bash: [[ -n $one ]]
Here is an improvement on miku's original answer that addresses Dennis Williamson's concerns about the case where the variable is not set:
the_world_is_flat=true
if ${the_world_is_flat:-false} ; then
echo "Be careful not to fall off!"
fi
And to test if the variable is false:
if ! ${the_world_is_flat:-false} ; then
echo "Be careful not to fall off!"
fi
About other cases with a nasty content in the variable, this is a problem with any external input fed to a program.
Any external input must be validated before trusting it. But that validation has to be done just once, when that input is received.
It doesn't have to impact the performance of the program by doing it on every use of the variable like Dennis Williamson suggests.
Here is a simple example which works for me:
temp1=true
temp2=false
if [ "$temp1" = true ] || [ "$temp2" = true ]
then
echo "Do something."
else
echo "Do something else."
fi
Here is an implementation of a short handed if true.
# Function to test if a variable is set to "true"
_if () {
[ "${1}" == "true" ] && return 0
[ "${1}" == "True" ] && return 0
[ "${1}" == "Yes" ] && return 0
return 1
}
Example 1
my_boolean=true
_if ${my_boolean} && {
echo "True Is True"
} || {
echo "False Is False"
}
Example 2
my_boolean=false
! _if ${my_boolean} && echo "Not True is True"
I found the existing answers confusing.
Personally, I just want to have something which looks and works like C.
This snippet works many times a day in production:
snapshotEvents=true
if ($snapshotEvents)
then
# Do stuff if true
fi
and to keep everyone happy, I tested:
snapshotEvents=false
if !($snapshotEvents)
then
# Do stuff if false
fi
Which also worked fine.
The $snapshotEvents evaluates the contents of value of the variable. So you need the $.
You don't really need the parentheses, I just find them helpful.
Tested on: GNU Bash, version 4.1.11(2)-release
Bash Guide for Beginners, Machtelt Garrels, v1.11, 2008
This is a speed test about different ways to test "Boolean" values in Bash:
#!/bin/bash
rounds=100000
b=true # For true; b=false for false
type -a true
time for i in $(seq $rounds); do command $b; done
time for i in $(seq $rounds); do $b; done
time for i in $(seq $rounds); do [ "$b" == true ]; done
time for i in $(seq $rounds); do test "$b" == true; done
time for i in $(seq $rounds); do [[ $b == true ]]; done
b=x; # Or any non-null string for true; b='' for false
time for i in $(seq $rounds); do [ "$b" ]; done
time for i in $(seq $rounds); do [[ $b ]]; done
b=1 # Or any non-zero integer for true; b=0 for false
time for i in $(seq $rounds); do ((b)); done
It would print something like
true is a shell builtin
true is /bin/true
real 0m0,815s
user 0m0,767s
sys 0m0,029s
real 0m0,562s
user 0m0,509s
sys 0m0,022s
real 0m0,829s
user 0m0,782s
sys 0m0,008s
real 0m0,782s
user 0m0,730s
sys 0m0,015s
real 0m0,402s
user 0m0,391s
sys 0m0,006s
real 0m0,668s
user 0m0,633s
sys 0m0,008s
real 0m0,344s
user 0m0,311s
sys 0m0,016s
real 0m0,367s
user 0m0,347s
sys 0m0,017s
You can use shFlags.
It gives you the option to define: DEFINE_bool
Example:
DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
From the command line you can define:
sh script.sh --bigmenu
sh script.sh --nobigmenu # False
In most cases you need "boolean" for boolean operations, such as ! && or ||. In some languages special boolean type do not exists at all (because in fact you do not need it technically), in most of interpreted languages (almost) all types automatically converted to some kind of boolean in operations like ! && or ||.
Boolean always is something that works with boolean operations. In Bash such operations are (no "" around $var, it's very important!):
[[ $var ]] - check if var is true
[[ ! $var ]] - check if var is false
[[ $var1 || $var2 ]] - var1 or var2
[[ $var1 && $var2 ]] - var1 and var2
[[ $var ]] is false only if var='' (or unset). So the only one correct 'false' value of variable in bash is '' (empty string). For true you can select any value, but I prefer var=1 (like in other languages, where true is any not-null int, but for readable purposes programmers always use 1).
NOTE: for var='false' var in boolean checks is true. In Bash string 'false' is actually true (same as in all other languages!). The only one difference between Bash and, e.g. Python, Perl or PHP, is that in Bash 0 is also true. But again: there are boolean operations in Bash, and they works like in all other interpreted languages, except that 0 in Bash is true.
It's absolutely irrational to use strings 'true' and 'false' as boolean replacement in Bash. It's like using same strange concept in Python, or Perl, or PHP. It works slower (in Bash too), and nobody do it in other languages, but in Bash there are a lot of coders that think it's a good solution. No. There are no reasons not to use boolean operations in Bash directly, without absolutely strange comparisons like $var == 'true' or $var == 'false'.
So again, boolean replacements in Bash are:
var='' # false
var=1 # true (actually any non-empty string)
And direct usage of boolean checks in Bash also is a fastest way to do boolean-like checks. All other constructions will works much slower.
P.S. "old" style of "boolean" checks like a=true; if $a; then ... are absolutely idiotic, because:
you can't use them directly in conditions [[ ]]
they act as eval of bash code, stored in variable. Well, if you think it's a good idea, just do not ever write any program, please;)
P.P.S. If $var can be unset and you use very helpful set -u just replace $var in checks to ${var-}, e.g. [[ ${var-} ]] (also: no "" around, it's important for speed of code execution!)
[[ "$x" == 'true' || "$x" -ne 0 ]] && ...
Is enough simple and has no dependencies.
Bash really confuses the issue with the likes of [, [[, ((, $((, etc.
All treading on each others' code spaces. I guess this is mostly historical, where Bash had to pretend to be sh occasionally.
Most of the time, I can just pick a method and stick with it. In this instance, I tend to declare (preferably in a common library file I can include with . in my actual script(s)).
TRUE=1; FALSE=0
I can then use the (( ... )) arithmetic operator to test thusly.
testvar=$FALSE
if [[ -d ${does_directory_exist} ]]
then
testvar=$TRUE;
fi
if (( testvar == TRUE )); then
# Do stuff because the directory does exist
fi
You do have to be disciplined. Your testvar must either be set to $TRUE or $FALSE at all times.
In (( ... )) comparators, you don't need the preceding $, which makes it more readable.
I can use (( ... )) because $TRUE=1 and $FALSE=0, i.e. numeric values.
The downside is having to use a $ occasionally:
testvar=$TRUE
which is not so pretty.
It's not a perfect solution, but it covers every case I need of such a test.
Alternative - use a function
is_ok(){ :;}
is_ok(){ return 1;}
is_ok && echo "It's OK" || echo "Something's wrong"
Defining the function is less intuitive, but checking its return value is very easy.
Related
Bash: `if ! [ $falseSetVar ] ` won't evaluate correctly for me
I have an if statement within a loop. It's set to false initially so I insert a timestamp in a file at the first run of the loop. I can't seem to get the following to evaluate correctly. $ConnectionIsCurrently=false if ! [ $ConnectionIsCurrently ]; then # changing false to true so this only occurs once. $ConnectionIsCurrently=true fi Here is the full loop: while [ $i -le $NoOfTests ]; do ping -c1 -t1 www.google.ie > /dev/null if [ $? = 0 ]; then ConTestPASSCount=$((ConTestPASSCount+1)) if ! [ $ConnectionIsCurrently ]; then printf 'PASSED AT: ' date "+%s" printf 'PASSED AT: ' >> $directory$LogFile date "+%s" >> $directory$LogFile ConnectionIsCurrently=true fi echo "PASSCount $ConTestPASSCount" else ConTestFAILCount=$((ConTestFAILCount+1)) if [ $ConnectionIsCurrently ]; then printf 'FAILED AT: ' date "+%s" printf 'FAILED AT: ' >> $directory$LogFile date "+%s" >> $directory$LogFile ConnectionIsCurrently=false fi echo "FAILCount $ConTestFAILCount" fi sleep 1 Testcount=$((Testcount+1)) i=$((i+1)) done
The shell doesn't have boolean values, it just operates on strings (or numbers in $(())). The syntax: if [ $ConnectionIsCurrently ] tests whether $ConnectionIsCurrently is a non-empty string, and "false" is not empty. You could use an empty value as falsey, and any non-empty value as truthy. ConnectionIsCurrently= if ! [ "$ConnectionIsCurrently" ]; then ConnectionIsCurrently=true fi Note also that you don't put $ before the variable name when you're assigning to it, only when you're reading it. And you should generally quote variables, unless you're sure you want word splitting done. This is especially important when the variable could be empty, as in this case; without the quotes, the [ command doesn't receive any parameter there.
false and true are actually commands (and also bash builtins), so you can run them as commands and act on the exit status: ConnectionIsCurrently=false if ! $ConnectionIsCurrently; then # changing false to true so this only occurs once. ConnectionIsCurrently=true fi The [...] are not required syntax for the if command: [ is just a regular command whose exit status is used by if.
To summarize: if and while execute a command and branch depending on whether that command succeeds or fails. false is a command that produces no output and always fails. true is a command that produces no output and always succeeds. [ is a command that succeeds or fails depending on the evaluation of the expression preceding the closing ] argument; man test or info test for details. With a single argument (which should be enclosed in double quotes) before the ], [ succeeds if and only if the argument is non-empty. The [ command is typically built into the shell, but it acts like a command; it's not a special shell syntax. The shell (sh, bash, ksh, zsh) does not have built-in Boolean types or values. There are several common idioms for using Booleans in shell scripts. A. Assign a variable the string value true or false. Using such a value in an if statement will do the right thing. (This method is my personal favorite.) Note that the strings true and false are the names of commands, not arbitrary strings. foo=true if $foo ; then echo OK ; else echo Oops ; fi B. Assign a variable any arbitrary non-empty value for truthiness, or the empty string (or leave it unset) for falsitude: foo=yes if [ "$foo" ] ; then echo OK ; else echo Oops ; fi foo="" if [ "$foo" ] ; then echo Oops ; else echo OK ; fi (The shell treats an unset variable as if it were set to the empty string -- unless you've done set -o nounset, but that's not usually done in scripts.) C. Pick two arbitrary strings to represent truth and falsehood, and use them consistently. Use string comparisons to test. foo=TRUE if [ "$foo" = TRUE ] ; then echo OK ; else echo Oops ; fi foo=FALSE if [ "$foo" = TRUE ] ; then echo Oops ; else echo OK ; fi All of these methods are potentially error-prone. If you forget a $ or misspell one of your conventional strings, you can get bad results with no warning from the shell; for example with method C, the string True will silently be treated as a false condition. Languages with strictly behaving Booleans can avoid these problems. Bash is not such a language.
Is unset parameter in [[ compound command expected as empty string in bash?
I am learning [[ compound command, that is, new if sentence. Some sites including bashFAQ/31 on What is the difference between test, [ and [[ ?, say that it does not need to quote parameter in it. My question is what exactly happened with unquoted unset parameter. I tried following code, unset UNDEF_VAR [[ ${UNDEF_VAR} = "" ]] echo "result is $?" and the output was result is 0 So, I expect that unset variable, ${UNDEF_VAR}, between [[ ]] is translated as empty string on the contrary to the case of old if, "[". But unfortunately, I could not find the explicit explanation in sites above and bash manual. Some sites just says it is OK without the reason. Someone who knows the mechanism, please let me know. If I missed the explanation in sites/manual carelessly, please forgive me for using your time. Thank you very much.
As far as I can find, the only reference to the expansion of an unset parameter is this sentence from the POSIX spec: The value, if any, of parameter shall be substituted. A consequence of this is that "$foo" is an empty string because no value is placed between the quotes. The bash man page has this to say about word-splitting: Explicit null arguments ("" or '') are retained. Unquoted implicit null arguments, resulting from the expansion of parameters that have no values, are removed. If a parameter with no value is expanded within double quotes, a null argument results and is retained. Inside [[ ... ]], a parameter expansion does not undergo word-splitting, so it is not clear exactly where "no value to substitute" becomes "null value". I assume that parameter expansions are treated as being implicitly quoted, whether or not they actually are.
The [[ version behaves much more like you would expect it (at least as I would). It's independent if the variable is unset or empty. $ unset foo $ [[ $foo == "" ]] && echo true || echo false true $ foo="" $ [[ $foo == "" ]] && echo true || echo false true With [ you have to be careful. You need to quote it, if there's any chance that it might be empty or unset: $ [ $foo = "" ] && echo true || echo false bash: [: =: unary operator expected false $ [ "$foo" = "" ] && echo true || echo false true Also note that when you use [[, you should compare with ==, and if you use [, compare with =. AFAIR zsh causes trouble, if you mix them. And of course, as #fedorqui noted, comparing empty strings should be done with -z.
Strange Bash if/elif behavior
I have the following script: a=$1 b=$2 if [ ! a ]; then echo "a=$a" elif [ b ]; then echo "b=$b" fi This is the four possible ways I can call it: balter$ bash eliftest.sh true true b=true balter$ bash eliftest.sh true false b=false balter$ bash eliftest.sh false true b=true balter$ bash eliftest.sh false false b=false I would have expected: b=true # !a evaluates to false but b is true <nothing> # !a evaluates to false and b is also false a=false # !a evaluates to true and if short circuits a=false # !a evaluates to true and if short circuits I clearly don't understand bash elif statements. Can someone enlighten me? Also, one the interwebs, bash scripting tutorials sometimes use [[...]] but mostly [...]. I see a lot of SO commenters saying that you should use [[...]]. Is there a rule of thumb for when one is better than the other?
You are making two mistakes: you want to use the variable a, and not the character "a"; so use $a. Since bash doesn't know booleans, you might just check if the value is "true". So you want something like this: if [ "$a" = "true" ]; then echo "a=$a" elif [ "$b" = "true" ]; then echo "b=$b" fi I implemented two more recommendations: always put variables between quotes; to avoid errors when they are empty ("$a" instead of $a) use only one = sign for portability (as suggested by #tripleee) some more detaills bash doesn't know boolean values. Your test: if [ ! a ]; will always fail since you ask bash: is the character "a" empty? Which is the same as: if [ "" = "a" ] suppose you did use $a (the variable): if [ ! $a ] this would still always be false; since this is equivalent to: if [ "" = "$a" ] which will never be true whether a=true or a=false (since a is never an empty string)
Avoid the brackets. Try this: a=$1 b=$2 if ! $a then echo "a=$a" elif $b then echo "b=$b" fi
Bash boolean expression and its value assignment
Is there a way to to evaluate a boolean expression and assign its value to a variable? In most of the scripting languages there is way to evaluates e.g //PHS $found= $count > 0 ; //evaluates to a boolean values I want similar way to evaluate in bash: BOOL=[ "$PROCEED" -ne "y" ] ; This is not working and tried other way but could not get a boolean value. IS there a way to do this WITHOUT using IF ?
You could do: [ "$PROCEED" = "y" ] ; BOOL=$? If you're working with set -e, you can use instead: [ "$PROCEED" = "y" ] && BOOL=0 || BOOL=1 BOOL set to zero when there is a match, to act like typical Unix return codes. Looks a bit weird. This will not throw errors, and you're sure $BOOL will be either 0 or 1 afterwards, whatever it contained before.
I would suggest: [ "$PROCEED" = "y" ] || BOOL=1 This has the advantage over checking $? that it works even when set -e is on. (See writing robust shell scripts.)
Rather than using ... && BOOL=0 || BOOL=1 suggested in the currently-accepted answer, it's clearer to use true and false. And since this question is about bash specifically (not POSIX shell), it's also better to use [[ instead of [ (see e.g. 1 and 2), which allows using == instead of =. So if you had to use a one-liner for something like this in bash, the following would be better: [[ "$PROCEED" == "y" ]] && should_proceed=true || should_proceed=false Then you can use the derived variable ergonomically in boolean contexts... if $should_proceed; then echo "Proceeding..." fi ...including with the ! operator: if ! $should_proceed; then echo "Bye for now." exit 0 fi
Assignment: found=$((count > 0)) For a boolean test: BOOL=$(test "$PROCEED" = y && echo true || echo false) In general, a x=$(...) assigns the output of ... to the variable x. The y does not need quotes, because it contains nothing which needs to be masked. A -ne is used for arithmetic comparison; see help test for an overview and quick reminder.
As explained in the accepted answer, the return value seems odd as true will return 0 and false 1. To make it easier to understand: #!/bin/bash test=$( [[ $1 == "y" ]]; echo $(($? == 0)) ) echo "$test" # It will print "1", otherwise "0". # To use it in conditions: if [ $test ]; then ... fi Another way is: test=$( [[ $1 == "y" ]] && echo "true" || echo "false" ) # In this case `[]` are not required: if $test; then ... fi
How can I declare and use Boolean variables in a shell script?
I tried to declare a Boolean variable in a shell script using the following syntax: variable=$false variable=$true Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using Boolean variables as expressions correct? if [ $variable ] if [ !$variable ]
Revised Answer (Feb 12, 2014) the_world_is_flat=true # ...do something interesting... if [ "$the_world_is_flat" = true ] ; then echo 'Be careful not to fall off!' fi Original Answer Caveats: https://stackoverflow.com/a/21210966/89391 the_world_is_flat=true # ...do something interesting... if $the_world_is_flat ; then echo 'Be careful not to fall off!' fi From: Using boolean variables in Bash The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson's comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.
TL;DR my_bool=true if [ "$my_bool" = true ] Issues with Miku's (original) answer I do not recommend the accepted answer1. Its syntax is pretty, but it has some flaws. Say we have the following condition. if $var; then echo 'Muahahaha!' fi In the following cases2, this condition will evaluate to true and execute the nested command. # Variable var not defined beforehand. Case 1 var='' # Equivalent to var="". # Case 2 var= # Case 3 unset var # Case 4 var='<some valid command>' # Case 5 Typically you only want your condition to evaluate to true when your "Boolean" variable, var in this example, is explicitly set to true. All the other cases are dangerously misleading! The last case (#5) is especially naughty because it will execute the command contained in the variable (which is why the condition evaluates to true for valid commands3, 4). Here is a harmless example: var='echo this text will be displayed when the condition is evaluated' if $var; then echo 'Muahahaha!' fi # Outputs: # this text will be displayed when the condition is evaluated # Muahahaha! Quoting your variables is safer, e.g. if "$var"; then. In the above cases, you should get a warning that the command is not found. But we can still do better (see my recommendations at the bottom). Also see Mike Holt's explanation of Miku's original answer. Issues with Hbar's answer This approach also has unexpected behavior. var=false if [ $var ]; then echo "This won't print, var is false!" fi # Outputs: # This won't print, var is false! You would expect the above condition to evaluate to false, thus never executing the nested statement. Surprise! Quoting the value ("false"), quoting the variable ("$var"), or using test or [[ instead of [, do not make a difference. What I do recommend: Here are ways I recommend you check your "Booleans". They work as expected. my_bool=true if [ "$my_bool" = true ]; then if [ "$my_bool" = "true" ]; then if [[ "$my_bool" = true ]]; then if [[ "$my_bool" = "true" ]]; then if [[ "$my_bool" == true ]]; then if [[ "$my_bool" == "true" ]]; then if test "$my_bool" = true; then if test "$my_bool" = "true"; then They're all pretty much equivalent. You'll have to type a few more keystrokes than the approaches in the other answers5, but your code will be more defensive. Footnotes Miku's answer has since been edited and no longer contains (known) flaws. Not an exhaustive list. A valid command in this context means a command that exists. It doesn't matter if the command is used correctly or incorrectly. E.g. man woman would still be considered a valid command, even if no such man page exists. For invalid (non-existent) commands, Bash will simply complain that the command wasn't found. If you care about length, the first recommendation is the shortest.
There seems to be some misunderstanding here about the Bash builtin true, and more specifically, about how Bash expands and interprets expressions inside brackets. The code in miku's answer has absolutely nothing to do with the Bash builtin true, nor /bin/true, nor any other flavor of the true command. In this case, true is nothing more than a simple character string, and no call to the true command/builtin is ever made, neither by the variable assignment, nor by the evaluation of the conditional expression. The following code is functionally identical to the code in the miku's answer: the_world_is_flat=yeah if [ "$the_world_is_flat" = yeah ]; then echo 'Be careful not to fall off!' fi The only difference here is that the four characters being compared are 'y', 'e', 'a', and 'h' instead of 't', 'r', 'u', and 'e'. That's it. There's no attempt made to call a command or builtin named yeah, nor is there (in miku's example) any sort of special handling going on when Bash parses the token true. It's just a string, and a completely arbitrary one at that. Update (2014-02-19): After following the link in miku's answer, now I see where some of the confusion is coming from. Miku's answer uses single brackets, but the code snippet he links to does not use brackets. It's just: the_world_is_flat=true if $the_world_is_flat; then echo 'Be careful not to fall off!' fi Both code snippets will behave the same way, but the brackets completely change what's going on under the hood. Here's what Bash is doing in each case: No brackets: Expand the variable $the_world_is_flat to the string "true". Attempt to parse the string "true" as a command. Find and run the true command (either a builtin or /bin/true, depending on the Bash version). Compare the exit code of the true command (which is always 0) with 0. Recall that in most shells, an exit code of 0 indicates success and anything else indicates failure. Since the exit code was 0 (success), execute the if statement's then clause Brackets: Expand the variable $the_world_is_flat to the string "true". Parse the now-fully-expanded conditional expression, which is of the form string1 = string2. The = operator is bash's string comparison operator. So... Do a string comparison on "true" and "true". Yep, the two strings were the same, so the value of the conditional is true. Execute the if statement's then clause. The no-brackets code works, because the true command returns an exit code of 0, which indicates success. The bracketed code works, because the value of $the_world_is_flat is identical to the string literal true on the right side of the =. Just to drive the point home, consider the following two snippets of code: This code (if run with root privileges) will reboot your computer: var=reboot if $var; then echo 'Muahahaha! You are going down!' fi This code just prints "Nice try." The reboot command is not called. var=reboot if [ $var ]; then echo 'Nice try.' fi Update (2014-04-14) To answer the question in the comments regarding the difference between = and ==: AFAIK, there is no difference. The == operator is a Bash-specific synonym for =, and as far as I've seen, they work exactly the same in all contexts. Note, however, that I'm specifically talking about the = and == string comparison operators used in either [ ] or [[ ]] tests. I'm not suggesting that = and == are interchangeable everywhere in bash. For example, you obviously can't do variable assignment with ==, such as var=="foo" (well technically you can do this, but the value of var will be "=foo", because Bash isn't seeing an == operator here, it's seeing an = (assignment) operator, followed by the literal value ="foo", which just becomes "=foo"). Also, although = and == are interchangeable, you should keep in mind that how those tests work does depend on whether you're using it inside [ ] or [[ ]], and also on whether or not the operands are quoted. You can read more about that in Advanced Bash Scripting Guide: 7.3 Other Comparison Operators (scroll down to the discussion of = and ==).
Use arithmetic expressions. #!/bin/bash false=0 true=1 ((false)) && echo false ((true)) && echo true ((!false)) && echo not false ((!true)) && echo not true Output: true not false
Long story short: There are no Booleans in Bash The true and false commands Bash does have Boolean expressions in terms of comparison and conditions. That said, what you can declare and compare in Bash are strings and numbers. That's it. Wherever you see true or false in Bash, it's either a string or a command/builtin which is only used for its exit code. This syntax... if true; then ... is essentially... if COMMAND; then ... where the command is true. The condition is true whenever the command returns exit code 0. true and false are Bash builtins and sometimes also standalone programs that do nothing but returning the corresponding exit code. Conditions in if..then..fi When using square brackets or the test command, you rely on the exit code of that construct. Keep in mind that [ ] and [[ ]] are also just commands/builtins like any other. So ... if [[ 1 == 1 ]]; then echo yes; fi corresponds to if COMMAND; then echo yes; fi and the COMMAND here is [[ with the parameters 1 == 1 ]] The if..then..fi construct is just syntactic sugar. You can always just run the commands separated by a double ampersand for the same effect: [[ 1 == 1 ]] && echo yes When using true and false in these testing constructs you are actually only passing the string "true" or "false" to the testing command. Here is an example: Believe it or not but those conditions are all yielding the same result: if [[ false ]]; then ... if [[ "false" ]]; then ... if [[ true ]]; then ... if [[ "true" ]]; then ... TL;DR; always compare against strings or numbers To make this clear to future readers, I would recommend always using quotes around true and false: DO if [[ "${var}" == "true" ]]; then ... if [[ "${var}" == "false" ]]; then ... if [[ "${var}" == "yes" ]]; then ... if [[ "${var}" == "USE_FEATURE_X" ]]; then ... if [[ -n "${var:-}" ]]; then echo "var is not empty" ... DON'T # Always use double square brackets in bash! if [ ... ]; then ... # This is not as clear or searchable as -n if [[ "${var}" ]]; then ... # Creates impression of Booleans if [[ "${var}" != true ]]; then ... # `-eq` is for numbers and doesn't read as easy as `==` if [[ "${var}" -eq "true" ]]; then ... Maybe # Creates impression of Booleans. # It can be used for strict checking of dangerous operations. # This condition is false for anything but the literal string "true". if [[ "${var}" != "true" ]]; then ...
Long ago, when all we had was sh, Booleans where handled by relying on a convention of the test program where test returns a false exit status if run without any arguments. This allows one to think of a variable that is unset as false and variable set to any value as true. Today, test is a builtin to Bash and is commonly known by its one-character alias [ (or an executable to use in shells lacking it, as dolmen notes): FLAG="up or <set>" if [ "$FLAG" ] ; then echo 'Is true' else echo 'Is false' fi # Unset FLAG # also works FLAG= if [ "$FLAG" ] ; then echo 'Continues true' else echo 'Turned false' fi Because of quoting conventions, script writers prefer to use the compound command [[ that mimics test, but has a nicer syntax: variables with spaces do not need to be quoted; one can use && and || as logical operators with weird precedence, and there are no POSIX limitations on the number of terms. For example, to determine if FLAG is set and COUNT is a number greater than 1: FLAG="u p" COUNT=3 if [[ $FLAG && $COUNT -gt '1' ]] ; then echo 'Flag up, count bigger than 1' else echo 'Nope' fi This stuff can get confusing when spaces, zero length strings, and null variables are all needed and also when your script needs to work with several shells.
Instead of faking a Boolean and leaving a trap for future readers, why not just use a better value than true and false? For example: build_state=success if something-horrible; then build_state=failed fi if [[ "$build_state" == success ]]; then echo go home; you are done else echo your head is on fire; run around in circles fi
How can I declare and use Boolean variables in a shell script? Unlike many other programming languages, Bash does not segregate its variables by "type." [1] So the answer is pretty clear. There isn't any Boolean variable in Bash. However: Using a declare statement, we can limit the value assignment to variables.[2] #!/bin/bash declare -ir BOOL=(0 1) # Remember BOOL can't be unset till this shell terminates readonly false=${BOOL[0]} readonly true=${BOOL[1]} # Same as declare -ir false=0 true=1 ((true)) && echo "True" ((false)) && echo "False" ((!true)) && echo "Not True" ((!false)) && echo "Not false" The r option in declare and readonly is used to state explicitly that the variables are readonly. I hope the purpose is clear.
My findings and suggestion differ a bit from the other posts. I found that I could use "booleans" basically as one would in any "regular" language, without the "hoop jumping" suggested... There isn't any need for [] or explicit string comparisons... I tried multiple Linux distributions. I tested Bash, Dash, and BusyBox. The results were always the same. I'm not sure what the original top voted posts are talking about. Maybe times have changed and that's all there is to it? If you set a variable to true, it subsequently evaluates as an "affirmative" within a conditional. Set it to false, and it evaluates to a "negative". Very straightforward! The only caveat, is that an undefined variable also evaluates like true! It would be nice if it did the opposite (as it would in most languages), but that's the trick - you just need to explicitly initialize your booleans to true or false. Why does it work this way? That answer is two fold. A) true/false in a shell really means "no error" vs "error" (i.e. 0 vs anything else). B) true/false are not values - but rather statements in shell scripting! Regarding the second point, executing true or false on a line by itself sets the return value for the block you're in to that value, i.e. false is a declaration of "error encountered", where true "clears" that. Using it with an assignment to a variable "returns" that into the variable. An undefined variable evaluates like true in a conditional because that equally represents 0 or "no error encountered". See the example Bash lines and results below. Test it yourself if you want to confirm... #!/bin/sh # Not yet defined... echo "when set to ${myBool}" if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi; myBool=true echo "when set to ${myBool}" if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi; myBool=false echo "when set to ${myBool}" if ${myBool}; then echo "it evaluates to true"; else echo "it evaluates to false"; fi; Yields when set to it evaluates to true when set to true it evaluates to true when set to false it evaluates to false
POSIX (Portable Operating System Interface) I miss here the key point, which is portability. That's why my header has POSIX in itself. Essentially, all of the voted answers are correct, with the exception they are Bash-specific too much. Basically, I only wish to add more information about portability. [ and ] brackets like in [ "$var" = true ] are not necessary, and you can omit them and use the test command directly: test "$var" = true && yourCodeIfTrue || yourCodeIfFalse Important note: I no longer recommend this as it's being slowly deprecated and more difficult to combine multiple statements. Imagine what those words true and false mean to the shell, test it yourself: echo $(( true )) 0 echo $(( false )) 1 But using quotes: echo $(( "true" )) bash: "true": syntax error: operand expected (error token is ""true"") sh (dash): sh: 1: arithmetic expression: expecting primary: ""true"" The same goes for: echo $(( "false" )) The shell can't interpret it other than a string. I hope you are getting the idea of how good it is using proper keyword without quotes. But no one said it in previous answers. What does this mean? Well, several things. You should get used to the Boolean keywords are actually treated like numbers, that is true = 0 and false = 1, remember all non-zero values are treated like false. Since they are treated as numbers, you should treat them like that too, i.e. if you define variable say: var_bool=true echo "$var_bool" true you can create an opposite value of it with: var_bool=$(( 1 - $var_bool )) # same as $(( ! $var_bool )) echo "$var_bool" 1 As you can see for yourself, the shell does print true string for the first time you use it, but since then, it all works via number 0 representing trueor 1 representing false, respectively. Finally, what you should do with all that information First, one good habit would be assigning 0 instead of true; 1 instead of false. Second good habit would be to test if the variable is / isn't equal to zero: if [ "$var_bool" -eq 0 ]; then yourCodeIfTrue else yourCodeIfFalse fi
In many programming languages, the Boolean type is, or is implemented as, a subtype of integer, where true behaves like 1 and false behaves like 0: Boolean in C Boolean in Python Boolean in Java Mathematically, Boolean algebra resembles integer arithmetic modulo 2. Therefore, if a language doesn't provide native Boolean type, the most natural and efficient solution is to use integers. This works with almost any language. For example, in Bash you can do: # val=1; ((val)) && echo "true" || echo "false" true # val=0; ((val)) && echo "true" || echo "false" false man bash: ((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
Regarding syntax, this is a simple methodology that I use (by example) to consistently and sanely manage Boolean logic: # Tests var= var='' var="" var=0 var=1 var="abc" var=abc if [[ -n "${var}" ]] ; then echo 'true' fi if [[ -z "${var}" ]] ; then echo 'false' fi # Results # var= # false # var='' # false # var="" # false # var=0 # true # var=1 # true # var="abc" # true # var=abc # true If the variable is never declared the answer is: # false So, a simple way to set a variable to true (using this syntax methodology) would be, var=1; conversely, var=''. Reference: -n = True if the length of var string is non-zero. -z = True if the length of var string is zero.
Bill Parker is getting voted down, because his definitions are reversed from the normal code convention. Normally, true is defined as 0 and false is defined as nonzero. 1 will work for false, as will 9999 and -1. The same with function return values - 0 is success and anything nonzero is failure. Sorry, I don't have the street credibility yet to vote or to reply to him directly. Bash recommends using double brackets now as a habit instead of single brackets, and the link Mike Holt gave explains the differences in how they work. 7.3. Other Comparison Operators For one thing, -eq is a numerical operator, so having the code #**** NOTE *** This gives error message ***** The_world_is_flat=0; if [ "${The_world_is_flat}" -eq true ]; then will issue an error statement, expecting an integer expression. This applies to either parameter, as neither is an integer value. Yet, if we put double brackets around it, it will not issue an error statement, but it will yield a wrong value (well, in 50% of the possible permutations). It will evaluate to [[0 -eq true]] = success, but also to [[0 -eq false]] = success, which is wrong (hmmm.... what about that builtin being a numerical value?). #**** NOTE *** This gives wrong output ***** The_world_is_flat=true; if [[ "${The_world_is_flat}" -eq true ]]; then There are other permutations of the conditional which will give wrong output as well. Basically, anything (other than the error condition listed above) that sets a variable to a numerical value and compares it to a true/false builtin, or sets a variable to a true/false builtin and compares it to a numerical value. Also, anything that sets a variable to a true/false builtin and does a comparison using -eq. So avoid -eq for Boolean comparisons and avoid using numerical values for Boolean comparisons. Here's a summary of the permutations that will give invalid results: # With variable set as an integer and evaluating to true/false # *** This will issue error warning and not run: ***** The_world_is_flat=0; if [ "${The_world_is_flat}" -eq true ]; then # With variable set as an integer and evaluating to true/false # *** These statements will not evaluate properly: ***** The_world_is_flat=0; if [ "${The_world_is_flat}" -eq true ]; then # if [[ "${The_world_is_flat}" -eq true ]]; then # if [ "${The_world_is_flat}" = true ]; then # if [[ "${The_world_is_flat}" = true ]]; then # if [ "${The_world_is_flat}" == true ]; then # if [[ "${The_world_is_flat}" == true ]]; then # With variable set as an true/false builtin and evaluating to true/false # *** These statements will not evaluate properly: ***** The_world_is_flat=true; if [[ "${The_world_is_flat}" -eq true ]]; then # if [ "${The_world_is_flat}" = 0 ]; then # if [[ "${The_world_is_flat}" = 0 ]]; then # if [ "${The_world_is_flat}" == 0 ]; then # if [[ "${The_world_is_flat}" == 0 ]]; then So, now to what works. Use true/false builtins for both your comparison and your evaluations (as Mike Hunt noted, don't enclose them in quotes). Then use either or single or double equal sign (= or ==) and either single or double brackets ([ ] or [[ ]]). Personally, I like the double equals sign, because it reminds me of logical comparisons in other programming languages, and double quotes just because I like typing. So these work: # With variable set as an integer and evaluating to true/false # *** These statements will work properly: ***** # The_world_is_flat=true/false; if [ "${The_world_is_flat}" = true ]; then # if [[ "${The_world_is_flat}" = true ]]; then # if [ "${The_world_is_flat}" = true ]; then # if [[ "${The_world_is_flat}" == true ]]; then There you have it.
My receipe to (my own) idiocy: # setting ---------------- commonMode=false if [[ $something == 'COMMON' ]]; then commonMode=true fi # using ---------------- if $commonMode; then echo 'YES, Common Mode' else echo 'NO, no Common Mode' fi $commonMode && echo 'commonMode is ON ++++++' $commonMode || echo 'commonMode is OFF xxxxxx'
Another way of using booleans is to test the emptyness of values. This has the advantage of making shorter tests: first=1 # A true value second= # A false value [ -n "$first" ] && echo 'First var is true' [ -z "$first" ] && echo 'First var is false' [ -n "$second" ] && echo 'Second var is true' [ -z "$second" ] && echo 'Second var is false' Output: First var is true Second var is false Here is an alternative test syntax with bash: [[ -n $one ]]
Here is an improvement on miku's original answer that addresses Dennis Williamson's concerns about the case where the variable is not set: the_world_is_flat=true if ${the_world_is_flat:-false} ; then echo "Be careful not to fall off!" fi And to test if the variable is false: if ! ${the_world_is_flat:-false} ; then echo "Be careful not to fall off!" fi About other cases with a nasty content in the variable, this is a problem with any external input fed to a program. Any external input must be validated before trusting it. But that validation has to be done just once, when that input is received. It doesn't have to impact the performance of the program by doing it on every use of the variable like Dennis Williamson suggests.
In most cases you need "boolean" for boolean operations, such as ! && or ||. In some languages special boolean type do not exists at all (because in fact you do not need it technically), in most of interpreted languages (almost) all types automatically converted to some kind of boolean in operations like ! && or ||. Boolean always is something that works with boolean operations. In Bash such operations are (no "" around $var, it's very important!): [[ $var ]] - check if var is true [[ ! $var ]] - check if var is false [[ $var1 || $var2 ]] - var1 or var2 [[ $var1 && $var2 ]] - var1 and var2 [[ $var ]] is false only if var='' (or unset). So the only one correct 'false' value of variable in bash is '' (empty string). For true you can select any value, but I prefer var=1 (like in other languages, where true is any not-null int, but for readable purposes programmers always use 1). NOTE: for var='false' var in boolean checks is true. In Bash string 'false' is actually true (same as in all other languages!). The only one difference between Bash and, e.g. Python, Perl or PHP, is that in Bash 0 is also true. But again: there are boolean operations in Bash, and they works like in all other interpreted languages, except that 0 in Bash is true. It's absolutely irrational to use strings 'true' and 'false' as boolean replacement in Bash. It's like using same strange concept in Python, or Perl, or PHP. It works slower (in Bash too), and nobody do it in other languages, but in Bash there are a lot of coders that think it's a good solution. No. There are no reasons not to use boolean operations in Bash directly, without absolutely strange comparisons like $var == 'true' or $var == 'false'. So again, boolean replacements in Bash are: var='' # false var=1 # true (actually any non-empty string) And direct usage of boolean checks in Bash also is a fastest way to do boolean-like checks. All other constructions will works much slower. P.S. "old" style of "boolean" checks like a=true; if $a; then ... are absolutely idiotic, because: you can't use them directly in conditions [[ ]] they act as eval of bash code, stored in variable. Well, if you think it's a good idea, just do not ever write any program, please;) P.P.S. If $var can be unset and you use very helpful set -u just replace $var in checks to ${var-}, e.g. [[ ${var-} ]] (also: no "" around, it's important for speed of code execution!)
Here is a simple example which works for me: temp1=true temp2=false if [ "$temp1" = true ] || [ "$temp2" = true ] then echo "Do something." else echo "Do something else." fi
Here is an implementation of a short handed if true. # Function to test if a variable is set to "true" _if () { [ "${1}" == "true" ] && return 0 [ "${1}" == "True" ] && return 0 [ "${1}" == "Yes" ] && return 0 return 1 } Example 1 my_boolean=true _if ${my_boolean} && { echo "True Is True" } || { echo "False Is False" } Example 2 my_boolean=false ! _if ${my_boolean} && echo "Not True is True"
I found the existing answers confusing. Personally, I just want to have something which looks and works like C. This snippet works many times a day in production: snapshotEvents=true if ($snapshotEvents) then # Do stuff if true fi and to keep everyone happy, I tested: snapshotEvents=false if !($snapshotEvents) then # Do stuff if false fi Which also worked fine. The $snapshotEvents evaluates the contents of value of the variable. So you need the $. You don't really need the parentheses, I just find them helpful. Tested on: GNU Bash, version 4.1.11(2)-release Bash Guide for Beginners, Machtelt Garrels, v1.11, 2008
This is a speed test about different ways to test "Boolean" values in Bash: #!/bin/bash rounds=100000 b=true # For true; b=false for false type -a true time for i in $(seq $rounds); do command $b; done time for i in $(seq $rounds); do $b; done time for i in $(seq $rounds); do [ "$b" == true ]; done time for i in $(seq $rounds); do test "$b" == true; done time for i in $(seq $rounds); do [[ $b == true ]]; done b=x; # Or any non-null string for true; b='' for false time for i in $(seq $rounds); do [ "$b" ]; done time for i in $(seq $rounds); do [[ $b ]]; done b=1 # Or any non-zero integer for true; b=0 for false time for i in $(seq $rounds); do ((b)); done It would print something like true is a shell builtin true is /bin/true real 0m0,815s user 0m0,767s sys 0m0,029s real 0m0,562s user 0m0,509s sys 0m0,022s real 0m0,829s user 0m0,782s sys 0m0,008s real 0m0,782s user 0m0,730s sys 0m0,015s real 0m0,402s user 0m0,391s sys 0m0,006s real 0m0,668s user 0m0,633s sys 0m0,008s real 0m0,344s user 0m0,311s sys 0m0,016s real 0m0,367s user 0m0,347s sys 0m0,017s
You can use shFlags. It gives you the option to define: DEFINE_bool Example: DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing"); From the command line you can define: sh script.sh --bigmenu sh script.sh --nobigmenu # False
[[ "$x" == 'true' || "$x" -ne 0 ]] && ... Is enough simple and has no dependencies.
Bash really confuses the issue with the likes of [, [[, ((, $((, etc. All treading on each others' code spaces. I guess this is mostly historical, where Bash had to pretend to be sh occasionally. Most of the time, I can just pick a method and stick with it. In this instance, I tend to declare (preferably in a common library file I can include with . in my actual script(s)). TRUE=1; FALSE=0 I can then use the (( ... )) arithmetic operator to test thusly. testvar=$FALSE if [[ -d ${does_directory_exist} ]] then testvar=$TRUE; fi if (( testvar == TRUE )); then # Do stuff because the directory does exist fi You do have to be disciplined. Your testvar must either be set to $TRUE or $FALSE at all times. In (( ... )) comparators, you don't need the preceding $, which makes it more readable. I can use (( ... )) because $TRUE=1 and $FALSE=0, i.e. numeric values. The downside is having to use a $ occasionally: testvar=$TRUE which is not so pretty. It's not a perfect solution, but it covers every case I need of such a test.
Alternative - use a function is_ok(){ :;} is_ok(){ return 1;} is_ok && echo "It's OK" || echo "Something's wrong" Defining the function is less intuitive, but checking its return value is very easy.