What is the difference between `if [[ .. ]] ; then …` and `[[ .. ]] && …`?












0















What is the difference between the if [[ ... ]] ; then ... and the [[ ... ]] && ... constructs in Bash, other than syntax? For example:



if [[ -d /etc ]] ; then echo "I see." ; else echo "I don't." ; fi
[[ -d /etc ]] && echo "I see." || echo "I don't."


What can one do that the other cannot?










share|improve this question


















  • 3





    It's BashPitfall #22.

    – Benjamin W.
    Jan 2 at 14:59











  • ...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

    – Charles Duffy
    Jan 2 at 15:02


















0















What is the difference between the if [[ ... ]] ; then ... and the [[ ... ]] && ... constructs in Bash, other than syntax? For example:



if [[ -d /etc ]] ; then echo "I see." ; else echo "I don't." ; fi
[[ -d /etc ]] && echo "I see." || echo "I don't."


What can one do that the other cannot?










share|improve this question


















  • 3





    It's BashPitfall #22.

    – Benjamin W.
    Jan 2 at 14:59











  • ...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

    – Charles Duffy
    Jan 2 at 15:02
















0












0








0








What is the difference between the if [[ ... ]] ; then ... and the [[ ... ]] && ... constructs in Bash, other than syntax? For example:



if [[ -d /etc ]] ; then echo "I see." ; else echo "I don't." ; fi
[[ -d /etc ]] && echo "I see." || echo "I don't."


What can one do that the other cannot?










share|improve this question














What is the difference between the if [[ ... ]] ; then ... and the [[ ... ]] && ... constructs in Bash, other than syntax? For example:



if [[ -d /etc ]] ; then echo "I see." ; else echo "I don't." ; fi
[[ -d /etc ]] && echo "I see." || echo "I don't."


What can one do that the other cannot?







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 14:26







user8370684















  • 3





    It's BashPitfall #22.

    – Benjamin W.
    Jan 2 at 14:59











  • ...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

    – Charles Duffy
    Jan 2 at 15:02
















  • 3





    It's BashPitfall #22.

    – Benjamin W.
    Jan 2 at 14:59











  • ...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

    – Charles Duffy
    Jan 2 at 15:02










3




3





It's BashPitfall #22.

– Benjamin W.
Jan 2 at 14:59





It's BashPitfall #22.

– Benjamin W.
Jan 2 at 14:59













...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

– Charles Duffy
Jan 2 at 15:02







...see also Ternary operator in bash?, especially the comment stackoverflow.com/questions/3953645/… and the answer by Ivan Pozdeev.

– Charles Duffy
Jan 2 at 15:02














2 Answers
2






active

oldest

votes


















3














There are not many differences, but one is that the first example is only using the first condition. In the second there are two conditions that effect the output.



if [[ -d /etc ]] ; # only condition that matters
then echo "I see." ;
else echo "I don't." ;
fi

[[ -d /etc ]] && # condition 1
echo "I see." || # condition 2
echo "I don't."


In this example condtion 2 will always be fine, as echo is a builtin function, but if it was another command, and it failed it would trigger the or clause, which is not possible in the first example. The first one only uses one condition the second relies on two conditions.






share|improve this answer



















  • 2





    Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

    – chepner
    Jan 2 at 14:38



















0














Addendum - It is a relevant aside that you can put multiple statements in each block of a properly formatted if/then/elif/else without much special effort (though it does take some finagling on the if part) but the ternary construct requires that you create blocks yourself.



if true && false || oops
then : "this shan't be executed"
: but I can put several statements anyway
elif true && true || oops
then echo hello
echo more statements!
else : I could put more statements here too
: see?
fi
bash: oops: command not found
hello
more statements!


To create multiple-statements blocks in a ternary you need something like this -



true && {
echo ok
echo more
oops!
} || false {
echo This still has the same logical vulnerability, now made *worse*
echo oh, woe.
}
ok
more
bash: oops!: command not found
echo This still has the same logical vulnerability
This still has the same logical vulnerability
echo oh, woe.
oh, woe.
}
bash: syntax error near unexpected token `}'


It could be cleaned up a bit, but that isn't the point.

They aren't the same.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008074%2fwhat-is-the-difference-between-if-then-and%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    There are not many differences, but one is that the first example is only using the first condition. In the second there are two conditions that effect the output.



    if [[ -d /etc ]] ; # only condition that matters
    then echo "I see." ;
    else echo "I don't." ;
    fi

    [[ -d /etc ]] && # condition 1
    echo "I see." || # condition 2
    echo "I don't."


    In this example condtion 2 will always be fine, as echo is a builtin function, but if it was another command, and it failed it would trigger the or clause, which is not possible in the first example. The first one only uses one condition the second relies on two conditions.






    share|improve this answer



















    • 2





      Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

      – chepner
      Jan 2 at 14:38
















    3














    There are not many differences, but one is that the first example is only using the first condition. In the second there are two conditions that effect the output.



    if [[ -d /etc ]] ; # only condition that matters
    then echo "I see." ;
    else echo "I don't." ;
    fi

    [[ -d /etc ]] && # condition 1
    echo "I see." || # condition 2
    echo "I don't."


    In this example condtion 2 will always be fine, as echo is a builtin function, but if it was another command, and it failed it would trigger the or clause, which is not possible in the first example. The first one only uses one condition the second relies on two conditions.






    share|improve this answer



















    • 2





      Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

      – chepner
      Jan 2 at 14:38














    3












    3








    3







    There are not many differences, but one is that the first example is only using the first condition. In the second there are two conditions that effect the output.



    if [[ -d /etc ]] ; # only condition that matters
    then echo "I see." ;
    else echo "I don't." ;
    fi

    [[ -d /etc ]] && # condition 1
    echo "I see." || # condition 2
    echo "I don't."


    In this example condtion 2 will always be fine, as echo is a builtin function, but if it was another command, and it failed it would trigger the or clause, which is not possible in the first example. The first one only uses one condition the second relies on two conditions.






    share|improve this answer













    There are not many differences, but one is that the first example is only using the first condition. In the second there are two conditions that effect the output.



    if [[ -d /etc ]] ; # only condition that matters
    then echo "I see." ;
    else echo "I don't." ;
    fi

    [[ -d /etc ]] && # condition 1
    echo "I see." || # condition 2
    echo "I don't."


    In this example condtion 2 will always be fine, as echo is a builtin function, but if it was another command, and it failed it would trigger the or clause, which is not possible in the first example. The first one only uses one condition the second relies on two conditions.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 14:35









    Nick EllisNick Ellis

    752818




    752818








    • 2





      Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

      – chepner
      Jan 2 at 14:38














    • 2





      Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

      – chepner
      Jan 2 at 14:38








    2




    2





    Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

    – chepner
    Jan 2 at 14:38





    Technically, echo can fail (for instance, if its standard output is redirected to a read-only file); it's just isn't common.

    – chepner
    Jan 2 at 14:38













    0














    Addendum - It is a relevant aside that you can put multiple statements in each block of a properly formatted if/then/elif/else without much special effort (though it does take some finagling on the if part) but the ternary construct requires that you create blocks yourself.



    if true && false || oops
    then : "this shan't be executed"
    : but I can put several statements anyway
    elif true && true || oops
    then echo hello
    echo more statements!
    else : I could put more statements here too
    : see?
    fi
    bash: oops: command not found
    hello
    more statements!


    To create multiple-statements blocks in a ternary you need something like this -



    true && {
    echo ok
    echo more
    oops!
    } || false {
    echo This still has the same logical vulnerability, now made *worse*
    echo oh, woe.
    }
    ok
    more
    bash: oops!: command not found
    echo This still has the same logical vulnerability
    This still has the same logical vulnerability
    echo oh, woe.
    oh, woe.
    }
    bash: syntax error near unexpected token `}'


    It could be cleaned up a bit, but that isn't the point.

    They aren't the same.






    share|improve this answer




























      0














      Addendum - It is a relevant aside that you can put multiple statements in each block of a properly formatted if/then/elif/else without much special effort (though it does take some finagling on the if part) but the ternary construct requires that you create blocks yourself.



      if true && false || oops
      then : "this shan't be executed"
      : but I can put several statements anyway
      elif true && true || oops
      then echo hello
      echo more statements!
      else : I could put more statements here too
      : see?
      fi
      bash: oops: command not found
      hello
      more statements!


      To create multiple-statements blocks in a ternary you need something like this -



      true && {
      echo ok
      echo more
      oops!
      } || false {
      echo This still has the same logical vulnerability, now made *worse*
      echo oh, woe.
      }
      ok
      more
      bash: oops!: command not found
      echo This still has the same logical vulnerability
      This still has the same logical vulnerability
      echo oh, woe.
      oh, woe.
      }
      bash: syntax error near unexpected token `}'


      It could be cleaned up a bit, but that isn't the point.

      They aren't the same.






      share|improve this answer


























        0












        0








        0







        Addendum - It is a relevant aside that you can put multiple statements in each block of a properly formatted if/then/elif/else without much special effort (though it does take some finagling on the if part) but the ternary construct requires that you create blocks yourself.



        if true && false || oops
        then : "this shan't be executed"
        : but I can put several statements anyway
        elif true && true || oops
        then echo hello
        echo more statements!
        else : I could put more statements here too
        : see?
        fi
        bash: oops: command not found
        hello
        more statements!


        To create multiple-statements blocks in a ternary you need something like this -



        true && {
        echo ok
        echo more
        oops!
        } || false {
        echo This still has the same logical vulnerability, now made *worse*
        echo oh, woe.
        }
        ok
        more
        bash: oops!: command not found
        echo This still has the same logical vulnerability
        This still has the same logical vulnerability
        echo oh, woe.
        oh, woe.
        }
        bash: syntax error near unexpected token `}'


        It could be cleaned up a bit, but that isn't the point.

        They aren't the same.






        share|improve this answer













        Addendum - It is a relevant aside that you can put multiple statements in each block of a properly formatted if/then/elif/else without much special effort (though it does take some finagling on the if part) but the ternary construct requires that you create blocks yourself.



        if true && false || oops
        then : "this shan't be executed"
        : but I can put several statements anyway
        elif true && true || oops
        then echo hello
        echo more statements!
        else : I could put more statements here too
        : see?
        fi
        bash: oops: command not found
        hello
        more statements!


        To create multiple-statements blocks in a ternary you need something like this -



        true && {
        echo ok
        echo more
        oops!
        } || false {
        echo This still has the same logical vulnerability, now made *worse*
        echo oh, woe.
        }
        ok
        more
        bash: oops!: command not found
        echo This still has the same logical vulnerability
        This still has the same logical vulnerability
        echo oh, woe.
        oh, woe.
        }
        bash: syntax error near unexpected token `}'


        It could be cleaned up a bit, but that isn't the point.

        They aren't the same.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:27









        Paul HodgesPaul Hodges

        3,7601524




        3,7601524






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008074%2fwhat-is-the-difference-between-if-then-and%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            Npm cannot find a required file even through it is in the searched directory

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith