Skip to main content
Made compliant with the Jon Skeet Decree - <https://twitter.com/PeterMortensen/status/976400000942034944>.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

Regarding syntax, this is a simple methodology that I use (by example) to consistently and sanely manage Boolean logic:

# testsTests
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

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

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.

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.

edited body
Source Link
Eric P
  • 598
  • 5
  • 9

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 ofif the length ifof var string is zero.

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 of the length if var string is zero.

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.

Added operator explanations
Source Link
Eric P
  • 598
  • 5
  • 9

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 of the length if var string is zero.

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

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 of the length if var string is zero.

Source Link
Eric P
  • 598
  • 5
  • 9
Loading