Skip to main content
removed irrelevant {} that might be confusing for new readers, spelling, structure + pointing out logical negation
Source Link
Vlastimil Burián
  • 3.3k
  • 4
  • 33
  • 55
  1. [ and ] brackets like in [ "${var}""$var" = true ] are not necessary, and you can omit them and use the test command directly:

     test "${var}""$var" = true && CodeIfTrueyourCodeIfTrue || CodeIfFalseyourCodeIfFalse
    

Note, thatImportant note: I no longer recommend this as it's being slowly deprecated and more difficult to combine multiple statements.

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

  • 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 likeas numbers, you should treat them like that too, i.e. if you define variable say:

         var_bool=true
         echo "${var_bool}""$var_bool"
    
     true
    

    you can create an opposite value of it with:

         var_bool=$(( 1 - $var_bool ))  # same as ${var_bool}(( ! $var_bool ))
         echo "${var_bool}""$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 true or 1 representing false, respectively.

  • 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}""$var_bool" -eq 0 ]; then 
     YourCodeIfTrue;    yourCodeIfTrue
    else 
     YourCodeIfFalse;    yourCodeIfFalse
    fi
    
  1. [ and ] brackets like in [ "${var}" = true ] are not necessary, and you can omit them and use the test command directly:

     test "${var}" = true && CodeIfTrue || CodeIfFalse
    

Note, that I no longer recommend this as it's being slowly deprecated.

The shell can't interpret it other than a string. I hope you are getting the idea of how good is using proper keyword without quotes.

  • 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 like 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} ))
         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 true or 1 representing false, respectively.

  • 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
    
  1. [ 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.

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.

  • 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 true or 1 representing false, respectively.

  • 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
    
Active reading [<http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

Essentially, all of the voted answers are correct, with the exception they are BASHBash-specific too much.

Essentially, all of the voted answers are correct, with the exception they are BASH-specific too much.

Essentially, all of the voted answers are correct, with the exception they are Bash-specific too much.

more experience, so edited accordingly to be up-to-date
Source Link
Vlastimil Burián
  • 3.3k
  • 4
  • 33
  • 55

So basicallyBasically, I only wish to add more information about portability.

  1. [ and ] brackets like in [ "$var""${var}" = true ] are not necessary, and you can omit them and use the test command directly:

     test "$var" = true && CodeIfTrue || CodeIfFalse
    
  2. Imagine what those words true and false mean to the shell, test it yourself:

     echo $((true))
    
     test "${var}" = true && CodeIfTrue || CodeIfFalse
    

Note, that I no longer recommend this as it's being slowly deprecated.

  1. Imagine what those words true and false mean to the shell, test it yourself:

     echo $(( true ))
    
0
0
    echo $((false))
    echo $(( false ))
1
1
    echo $(("true"))
    echo $(( "true" ))
bash: "true": syntax error: operand expected (error token is ""true"")
bash: "true": syntax error: operand expected (error token is ""true"")
    sh (dash): sh: 1: arithmetic expression: expecting primary: ""true""
    sh (dash): sh: 1: arithmetic expression: expecting primary: ""true""
    echo $(("false"))
    echo $(( "false" ))
  1. What does this meansmean? 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 like numbers, you should treat them like that too, i.e. if you define variable say:

         var_a=true
         echo "$var_a"
    
         var_bool=true
         echo "${var_bool}"
    
     true
    
     true
    

    you can create an opposite value of it with:

         var_a=$((1 - $var_a))
         echo "$var_a"
    
         var_bool=$(( 1 - ${var_bool} ))
         echo "${var_bool}"
    
    1
    
    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 orrepresenting true or 1 representing false, respectively.

  • 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:

      test "$var" -eq 0 && CodeIfTrue || CodeIfFalse
    
      if [ "${var_bool}" -eq 0 ]; then YourCodeIfTrue; else YourCodeIfFalse; fi
    

So basically, I only wish to add more information about portability.

  1. [ and ] brackets like in [ "$var" = true ] are not necessary, and you can omit them and use the test command directly:

     test "$var" = true && CodeIfTrue || CodeIfFalse
    
  2. Imagine what those words true and false mean to the shell, test it yourself:

     echo $((true))
    
0
    echo $((false))
1
    echo $(("true"))
bash: "true": syntax error: operand expected (error token is ""true"")
    sh (dash): sh: 1: arithmetic expression: expecting primary: ""true""
    echo $(("false"))
  1. What this means? 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 like numbers, you should treat them like that too, i.e. if you define variable say:

         var_a=true
         echo "$var_a"
    
     true
    

    you can create an opposite value of it with:

         var_a=$((1 - $var_a))
         echo "$var_a"
    
    1
    

    As you can see for yourself, shell does print true string for the first time you use it, but since then, it all works via number 0 or 1, respectively.

  • First 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:

      test "$var" -eq 0 && CodeIfTrue || CodeIfFalse
    

Basically, I only wish to add more information about portability.

  1. [ and ] brackets like in [ "${var}" = true ] are not necessary, and you can omit them and use the test command directly:

     test "${var}" = true && CodeIfTrue || CodeIfFalse
    

Note, that I no longer recommend this as it's being slowly deprecated.

  1. Imagine what those words true and false mean to the shell, test it yourself:

     echo $(( true ))
    
0
    echo $(( false ))
1
    echo $(( "true" ))
bash: "true": syntax error: operand expected (error token is ""true"")
    sh (dash): sh: 1: arithmetic expression: expecting primary: ""true""
    echo $(( "false" ))
  1. 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 like 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} ))
         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 true or 1 representing false, respectively.

  • 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
    
Source Link
Vlastimil Burián
  • 3.3k
  • 4
  • 33
  • 55
Loading