Skip to main content
formatting, mostly
Source Link
Hugo G
  • 16.1k
  • 7
  • 61
  • 78

The true and false commands

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.

The conditional above is equivalent to:

COMMAND && ...

Conditions in if..then..fi

and the COMMAND here is [[ with the parameters 1 == 1 ]]

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" ...
if [ ... ]; then ...  # Always use double square brackets in bash!
if [[[ "${var}"... ]];]; then ...   
# This is not as clear or searchable as -n
if [[ "${var}" != true ]]; then ...   
# Creates impression of Booleans
if [[ "${var}" -eq!= "true"true ]]; then ...   
# `-eq` is for numbers and doesn't read as easy as `==`

Maybe

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

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.

The conditional above is equivalent to:

COMMAND && ...

and the COMMAND here is [[ 1 == 1 ]]

if [[ "${var}" == "true" ]]; then ...
if [[ "${var}" == "false" ]]; then ...
if [[ -n "${var:-}" ]]; then echo "var is not empty" ...
if [ ... ]; then ...  # Always use double square brackets in bash!
if [[ "${var}" ]]; then ...  # This is not as clear or searchable as -n
if [[ "${var}" != true ]]; then ...  # Creates impression of Booleans
if [[ "${var}" -eq "true" ]]; then ...  # `-eq` is for numbers and doesn't read as easy as `==`

Maybe

if [[ "${var}" != "true" ]]; then ...  # 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".

The true and false commands

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

and the COMMAND here is [[ with the parameters 1 == 1 ]]

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" ...
# 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 ... 
Active reading [<https://en.wiktionary.org/wiki/Boolean#Noun> <http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>]. Made compliant with the Jon Skeet Decree - <https://twitter.com/PeterMortensen/status/976400000942034944>.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

There are no booleansBooleans in bashBash

What bashBash does have, is boolean Boolean expressions in terms of comparison and conditions. That said, what you can declare and compare in bashBash are strings and numbers. That's it.

Wherever you see true or false in bashBash, it's either a string or a command/builtin which is only used for its exit code.

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:

if [ ... ]; then ...  # alwaysAlways use double square brackets in bash!
if [[ "${var}" ]]; then ...  # thisThis is not as clear or searchable as -n
if [[ "${var}" != true ]]; then ...  # createsCreates impression of booleansBooleans
if [[ "${var}" -eq "true" ]]; then ...  # `-eq` is for numbers and doesn't read as easy as `==`
if [[ "${var}" != "true" ]]; then ...  # createsCreates impression of booleansBooleans. CanIt can be used for strict checking of dangerous operations. This condition is false for anything but the literal string "true". 

There are no booleans in bash

What bash does have, is 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.

When using true and false in these testing constructs you are actually only passing the string "true" or "false" to the testing command. Here an example:

if [ ... ]; then ...  # always use double square brackets in bash!
if [[ "${var}" ]]; then ...  # this is not as clear or searchable as -n
if [[ "${var}" != true ]]; then ...  # creates impression of booleans
if [[ "${var}" -eq "true" ]]; then ...  # `-eq` is for numbers and doesn't read as easy as `==`
if [[ "${var}" != "true" ]]; then ...  # creates impression of booleans. Can be used for strict checking of dangerous operations. This condition is false for anything but the literal string "true". 

There are no Booleans in Bash

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.

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:

if [ ... ]; then ...  # Always use double square brackets in bash!
if [[ "${var}" ]]; then ...  # This is not as clear or searchable as -n
if [[ "${var}" != true ]]; then ...  # Creates impression of Booleans
if [[ "${var}" -eq "true" ]]; then ...  # `-eq` is for numbers and doesn't read as easy as `==`
if [[ "${var}" != "true" ]]; then ...  # 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".
added 313 characters in body
Source Link
Hugo G
  • 16.1k
  • 7
  • 61
  • 78

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.

You could just as well do thisThe conditional above is equivalent to:

if which foo; then echo "program fooCOMMAND found";&& fi...
 

corresponds to

if COMMAND; then echo yes; fi

and the COMMAND here is [[ 1 == 1 ]]

The if..then..fi construct is just syntactic sugar for ... You can always just run the commands separated by a double ampersand for the same effect:

So whenWhen using true and false in any of the aforementionedthese testing constructs you are actually only passing the string "true" or "false" to the testing command. Here an example:

The condition is true whenever the command returns exit code 0.

You could just as well do this:

if which foo; then echo "program foo found"; fi

is just syntactic sugar for ...

So when using true and false in any of the aforementioned constructs you are actually only passing the string "true" or "false" to the testing command. Here an example:

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.

The conditional above is equivalent to:

COMMAND && ...
 

corresponds to

if COMMAND; then echo yes; fi

and the COMMAND here is [[ 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:

When using true and false in these testing constructs you are actually only passing the string "true" or "false" to the testing command. Here an example:

added 1 character in body
Source Link
Hugo G
  • 16.1k
  • 7
  • 61
  • 78
Loading
added 103 characters in body
Source Link
Hugo G
  • 16.1k
  • 7
  • 61
  • 78
Loading
Source Link
Hugo G
  • 16.1k
  • 7
  • 61
  • 78
Loading