Equivalence of "if then" statements in bash

Would these two sections of code be equivalent?
The original is the first one, followed by the new
implementation.

  if ["$num1" -lt "$pmin"]; Then 
     ... 
  else   
    if ["$num2" -lt "$pmin"]; Then 
       ... 
    fi  
  fi 
  if ["$num1" -lt "$pmin"]; Then 
     ... 
  else if ["$num2" -lt "$pmin"]; Then 
       ... 
  fi 

How would they ?

Instead of else if , use elif , then it should work, provided you use a lowercase T in then and you use spaces around the square brackets...

1 Like

The second example is missing an fi ; you have 2 if s, but only one fi . Better is, as Scrutinizer pointed out, to use elif ...; then .

In bash, you can use:

if (( num < pmin )); then
if (( num1 < pmin )); then

An 'else if' and an 'elif' are two different things.

Here are some basic if/else examples

if true
then	echo true
fi

true && echo true

if ! false
then	echo true
else	echo false
fi

! false && echo true || echo false
! false && \
	echo true || \
	echo false

if true
then	if false
	then	echo true-false
	else	echo true-true
	fi
fi

if true
then	if false
	then	echo true-false
	else	echo true-true
	fi
else	echo false
fi

if true
then	echo true
elif false
then	echo false
else	echo something else?
fi