help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bash 'if [[ <cond> ]]; then ...fi' should 'then' be redundant?


From: Vang Le-Quy
Subject: Re: bash 'if [[ <cond> ]]; then ...fi' should 'then' be redundant?
Date: Thu, 7 May 2020 13:39:52 +0000

Hov, I haven't thought or paid attention about that. Thanks for eye opening. I 
played with this convoluted example to understand. However, it shows clear sign 
that this style of coding will cloud the logic comparing to strick chain of 
logical operators for `if` and `elif`
==========
if
var=1
val=3
var=$(($var+1))
var=$(($var+2))
[ $var -eq $val ]
then
  false
  echo "can be reached, if \$val == 4"
elif
  false
  true
  false
then
  echo "not print, becuase last command of elif is false"
else
  echo 'at last, should print when $val != 4'
  echo var: $var, val: $val
fi
===========

On 07/05/2020, 14.39, "Greg Wooledge" <address@hidden> wrote:

    On Thu, May 07, 2020 at 12:05:41PM +0000, Vang Le-Quy wrote:
    > if  [[ <condition> ]]; then
    >                 statements
    > else
    >                 statements
    > fi
    > 
    > ```
    > 
    > There might be a historical reason for `then` to be in there. But would 
it just be straight forward without `then`? What is ambiguous about it?
    
    The actual syntax is:
    
    if COMMAND LIST ONE
    then COMMAND LIST TWO
    else COMMAND LIST THREE
    fi
    
    You can have more than one command in each list.  A command whose first
    word is "then" or "else" or "fi" or "elif" signifies the end of a
    list.
    
    Silly example:
    
    if true; false; true; then echo yes; fi
    
    Slightly better example:
    
    if tmp=${input##*\[}; tmp=${tmp%%\]*; [[ $tmp = ABC ]]; then echo match; fi
    


reply via email to

[Prev in Thread] Current Thread [Next in Thread]