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:59:39 +0000

So it is like this

If do-command-list-and-if-the-last-command-returns-true
then
      DO-WHATEVER1
elif1 
in-case-THE-IF-returns-false-and-do-command-list-and-if-the-last-command-returns-true
then
      DO-WHATEVER2
elif2 
in-case-THE-ELIF1-returns-false-and-do-command-list-and-if-the-last-command-returns-true
then
        DO-WHATEVER3
else
        DO-WHATEVER4
fi

However, I would prefer putting all of those lengthy list of command in 
functions and exposure the logic:

if [ func1 = true ]; then
     DO-WHATEVER1
elif [ func2 = true ]; then
    DO-WHATEVER2
elif [ func3 = true ]; then
    DO-WHATEVER3
else
    DO-WHATEVER4
fi

Then again, if this structure, the `then` look redundant.


On 07/05/2020, 14.44, "Chet Ramey" <address@hidden> wrote:

    On 5/7/20 8:05 AM, Vang Le-Quy wrote:
    > Ever since I wrote my first bash `if`, I was told to immediately use 
`then`. This structure become so familiar:
    > 
    > ```
    > 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 command after the `if' is any command list, not just a conditional
    command. You use `then' to separate it from the true case command list.
    
    -- 
    ``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
    Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/
    


reply via email to

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