Skip to main content
Improve formatting of cases - numbered list instead of code block. Unless I'm horribly mistaken, `var=` is equivalent to `var=""`.
Source Link
wjandrea
  • 31.6k
  • 9
  • 67
  • 88
# 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
  1. Variable var not defined beforehand
  2. var='' (equivalent to var="" or var=)
  3. unset var
  4. var='<some valid command>'

The last case (#5#4) 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).

# 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

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

  1. Variable var not defined beforehand
  2. var='' (equivalent to var="" or var=)
  3. unset var
  4. var='<some valid command>'

The last case (#4) 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).

Better distinguishing the variable name from the name of the type. It's confusing when the variable name is the same as the name of a type in some programming languages.
Source Link
einpoklum
  • 126.7k
  • 69
  • 385
  • 793
bool=truemy_bool=true

if [ "$bool""$my_bool" = true ]
bool=truemy_bool=true

if [ "$bool""$my_bool" = true ]; then
if [ "$bool""$my_bool" = "true" ]; then

if [[ "$bool""$my_bool" = true ]]; then
if [[ "$bool""$my_bool" = "true" ]]; then
if [[ "$bool""$my_bool" == true ]]; then
if [[ "$bool""$my_bool" == "true" ]]; then

if test "$bool""$my_bool" = true; then
if test "$bool""$my_bool" = "true"; then
bool=true

if [ "$bool" = true ]
bool=true

if [ "$bool" = true ]; then
if [ "$bool" = "true" ]; then

if [[ "$bool" = true ]]; then
if [[ "$bool" = "true" ]]; then
if [[ "$bool" == true ]]; then
if [[ "$bool" == "true" ]]; then

if test "$bool" = true; then
if test "$bool" = "true"; then
my_bool=true

if [ "$my_bool" = true ]
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
Changed Grammar mistakes and comment positioning
Source Link
tgdavies
  • 11.2k
  • 4
  • 38
  • 42
var='echo this text will be displayed when the condition is evaluated'
if $var; then
  echo 'Muahahaha!'
fi

# Outputs:
# Thisthis text will be displayed when the condition is evaluated
# Muahahaha!
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!
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!
Loading
Used a more direct cross reference, etc.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132
Loading
Active reading [<https://en.wiktionary.org/wiki/Boolean#Noun>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132
Loading
Rollback to Revision 8
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading
Active reading [<https://en.wiktionary.org/wiki/Boolean#Noun>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132
Loading
fixed formatting
Source Link
Vadim Kotov
  • 8.2k
  • 8
  • 49
  • 63
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
added 39 characters in body; added 5 characters in body
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading
deleted 142 characters in body
Source Link
chepner
  • 520.5k
  • 75
  • 570
  • 721
Loading
Link to Mike Holt's explanation of Miku's answer
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading
Point out issues in Hbar's answer
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading
Be a bit more specific.
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading
Source Link
Dennis
  • 58.7k
  • 26
  • 146
  • 142
Loading