Why does ! operator with set -e always succeed even for truthy return values?












2















I use a system where scripts utilize set -e:



#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached


If I do this:



#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached


...the last line is reached. However, if I do this:



#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached


...the last line is reached even though ! true should be false.



More testing:



user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$


...so in these cases, the ! operator works correctly.



However, it does not work properly with set -e. Why?



Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.










share|improve this question



























    2















    I use a system where scripts utilize set -e:



    #!/bin/sh
    set -e
    echo 1
    true
    echo 2
    true
    echo 3
    false
    echo notreached


    If I do this:



    #!/bin/sh
    set -e
    echo 1
    true
    echo 2
    true
    echo 3
    ! false
    echo reached


    ...the last line is reached. However, if I do this:



    #!/bin/sh
    set -e
    echo 1
    true
    echo 2
    true
    echo 3
    ! true
    echo reached


    ...the last line is reached even though ! true should be false.



    More testing:



    user@linux:~$ ! false && echo ok
    ok
    user@linux:~$ ! true && echo ok
    user@linux:~$


    ...so in these cases, the ! operator works correctly.



    However, it does not work properly with set -e. Why?



    Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.










    share|improve this question

























      2












      2








      2








      I use a system where scripts utilize set -e:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      false
      echo notreached


      If I do this:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      ! false
      echo reached


      ...the last line is reached. However, if I do this:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      ! true
      echo reached


      ...the last line is reached even though ! true should be false.



      More testing:



      user@linux:~$ ! false && echo ok
      ok
      user@linux:~$ ! true && echo ok
      user@linux:~$


      ...so in these cases, the ! operator works correctly.



      However, it does not work properly with set -e. Why?



      Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.










      share|improve this question














      I use a system where scripts utilize set -e:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      false
      echo notreached


      If I do this:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      ! false
      echo reached


      ...the last line is reached. However, if I do this:



      #!/bin/sh
      set -e
      echo 1
      true
      echo 2
      true
      echo 3
      ! true
      echo reached


      ...the last line is reached even though ! true should be false.



      More testing:



      user@linux:~$ ! false && echo ok
      ok
      user@linux:~$ ! true && echo ok
      user@linux:~$


      ...so in these cases, the ! operator works correctly.



      However, it does not work properly with set -e. Why?



      Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.







      bash shell-script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      juhistjuhist

      1333




      1333






















          1 Answer
          1






          active

          oldest

          votes


















          7














          Because that's what the standard says:




          -e

          When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:




          1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
            beginning with the ! reserved word
            , or any command of an AND-OR list
            other than the last.




          http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set



          Bash's manual also points this out, with somewhat different wording.



          I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f496316%2fwhy-does-operator-with-set-e-always-succeed-even-for-truthy-return-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            Because that's what the standard says:




            -e

            When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:




            1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
              beginning with the ! reserved word
              , or any command of an AND-OR list
              other than the last.




            http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set



            Bash's manual also points this out, with somewhat different wording.



            I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.






            share|improve this answer






























              7














              Because that's what the standard says:




              -e

              When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:




              1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
                beginning with the ! reserved word
                , or any command of an AND-OR list
                other than the last.




              http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set



              Bash's manual also points this out, with somewhat different wording.



              I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.






              share|improve this answer




























                7












                7








                7







                Because that's what the standard says:




                -e

                When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:




                1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
                  beginning with the ! reserved word
                  , or any command of an AND-OR list
                  other than the last.




                http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set



                Bash's manual also points this out, with somewhat different wording.



                I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.






                share|improve this answer















                Because that's what the standard says:




                -e

                When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:




                1. The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
                  beginning with the ! reserved word
                  , or any command of an AND-OR list
                  other than the last.




                http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set



                Bash's manual also points this out, with somewhat different wording.



                I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 1 hour ago









                ilkkachuilkkachu

                57k785158




                57k785158






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f496316%2fwhy-does-operator-with-set-e-always-succeed-even-for-truthy-return-values%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

                    SQL Server 17 - Attemping to backup to remote NAS but Access is denied

                    Always On Availability groups resolving state after failover - Remote harden of transaction...

                    Restoring from pg_dump with foreign key constraints