Skip to main content
added 212 characters in body
Source Link

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 'programmers'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:

  1. you can't use them directly in conditions [[ ]]
  2. 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!)

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, but in Bash there are a lot of 'programmers' 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:

  1. you can't use them directly in conditions [[ ]]
  2. 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;)

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:

  1. you can't use them directly in conditions [[ ]]
  2. 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!)

added 174 characters in body
Source Link

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 ||.

So. 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. NobodyIt works slower (in Bash too), and nobody do it, but in Bash there are a lot of 'programmers' 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:

  1. you can't use them directly in conditions [[ ]]
  2. 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;)

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 ||.

So. Boolean always is something that works with boolean operations. In Bash such operations are:

[[ $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. Nobody do it, but in Bash there are a lot of 'programmers' 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)

P.S. "old" style of "boolean" checks like a=true; if $a; then ... are absolutely idiotic, because:

  1. you can't use them directly in conditions [[ ]]
  2. 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;)

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, but in Bash there are a lot of 'programmers' 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:

  1. you can't use them directly in conditions [[ ]]
  2. 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;)
Source Link

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 ||.

So. Boolean always is something that works with boolean operations. In Bash such operations are:

[[ $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. Nobody do it, but in Bash there are a lot of 'programmers' 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)

P.S. "old" style of "boolean" checks like a=true; if $a; then ... are absolutely idiotic, because:

  1. you can't use them directly in conditions [[ ]]
  2. 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;)