How to group multiple conditions in an if statement in fish

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












As is, the code below is invalid, because the brackets can not be used like that. if we remove them, it runs fine, and outputs:



true
true


code:



#!/usr/bin/fish

if ( false ; and true ) ; or true
echo "true"
else
echo "false"
end

if false ; and ( true ; or true )
echo "true"
else
echo "false"
end


How to get the functionality indicated by the brackets?



desired output:



true
false






share|improve this question
























    up vote
    2
    down vote

    favorite












    As is, the code below is invalid, because the brackets can not be used like that. if we remove them, it runs fine, and outputs:



    true
    true


    code:



    #!/usr/bin/fish

    if ( false ; and true ) ; or true
    echo "true"
    else
    echo "false"
    end

    if false ; and ( true ; or true )
    echo "true"
    else
    echo "false"
    end


    How to get the functionality indicated by the brackets?



    desired output:



    true
    false






    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      As is, the code below is invalid, because the brackets can not be used like that. if we remove them, it runs fine, and outputs:



      true
      true


      code:



      #!/usr/bin/fish

      if ( false ; and true ) ; or true
      echo "true"
      else
      echo "false"
      end

      if false ; and ( true ; or true )
      echo "true"
      else
      echo "false"
      end


      How to get the functionality indicated by the brackets?



      desired output:



      true
      false






      share|improve this question












      As is, the code below is invalid, because the brackets can not be used like that. if we remove them, it runs fine, and outputs:



      true
      true


      code:



      #!/usr/bin/fish

      if ( false ; and true ) ; or true
      echo "true"
      else
      echo "false"
      end

      if false ; and ( true ; or true )
      echo "true"
      else
      echo "false"
      end


      How to get the functionality indicated by the brackets?



      desired output:



      true
      false








      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 27 at 12:53









      hoijui

      1217




      1217




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use begin and end for conditionals as well:



          From fish tutorial:




          For even more complex conditions, use begin and end to group parts of them.




          For a simpler example, you can take a look at this answer from stackoverflow.



          For your code, you just have to replace the ( with begin ; and the ) with ; end.



          #!/usr/bin/fish

          if begin ; false ; and true ; end ; or true
          echo "true"
          else
          echo "false"
          end

          if false; and begin ; true ; or true ; end
          echo "true"
          else
          echo "false"
          end





          share|improve this answer






















          • ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
            – hoijui
            Feb 27 at 14:04


















          up vote
          0
          down vote













          Alternative solution: outsource part of the conditional chain into a function



          like so:



          #!/usr/bin/fish

          function _my_and_checker
          return $argv[1]; and argv[2]
          end
          function _my_or_checker
          return $argv[1]; or argv[2]
          end

          if _my_and_checker false true ; or true
          echo "true"
          else
          echo "false"
          end

          if false; and _my_or_checker true true
          echo "true"
          else
          echo "false"
          end


          This makes most sense if the conditions themselves are complex commands.






          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f426928%2fhow-to-group-multiple-conditions-in-an-if-statement-in-fish%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You can use begin and end for conditionals as well:



            From fish tutorial:




            For even more complex conditions, use begin and end to group parts of them.




            For a simpler example, you can take a look at this answer from stackoverflow.



            For your code, you just have to replace the ( with begin ; and the ) with ; end.



            #!/usr/bin/fish

            if begin ; false ; and true ; end ; or true
            echo "true"
            else
            echo "false"
            end

            if false; and begin ; true ; or true ; end
            echo "true"
            else
            echo "false"
            end





            share|improve this answer






















            • ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
              – hoijui
              Feb 27 at 14:04















            up vote
            3
            down vote



            accepted










            You can use begin and end for conditionals as well:



            From fish tutorial:




            For even more complex conditions, use begin and end to group parts of them.




            For a simpler example, you can take a look at this answer from stackoverflow.



            For your code, you just have to replace the ( with begin ; and the ) with ; end.



            #!/usr/bin/fish

            if begin ; false ; and true ; end ; or true
            echo "true"
            else
            echo "false"
            end

            if false; and begin ; true ; or true ; end
            echo "true"
            else
            echo "false"
            end





            share|improve this answer






















            • ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
              – hoijui
              Feb 27 at 14:04













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            You can use begin and end for conditionals as well:



            From fish tutorial:




            For even more complex conditions, use begin and end to group parts of them.




            For a simpler example, you can take a look at this answer from stackoverflow.



            For your code, you just have to replace the ( with begin ; and the ) with ; end.



            #!/usr/bin/fish

            if begin ; false ; and true ; end ; or true
            echo "true"
            else
            echo "false"
            end

            if false; and begin ; true ; or true ; end
            echo "true"
            else
            echo "false"
            end





            share|improve this answer














            You can use begin and end for conditionals as well:



            From fish tutorial:




            For even more complex conditions, use begin and end to group parts of them.




            For a simpler example, you can take a look at this answer from stackoverflow.



            For your code, you just have to replace the ( with begin ; and the ) with ; end.



            #!/usr/bin/fish

            if begin ; false ; and true ; end ; or true
            echo "true"
            else
            echo "false"
            end

            if false; and begin ; true ; or true ; end
            echo "true"
            else
            echo "false"
            end






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 27 at 13:14

























            answered Feb 27 at 12:59









            Stefan M

            8101617




            8101617











            • ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
              – hoijui
              Feb 27 at 14:04

















            • ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
              – hoijui
              Feb 27 at 14:04
















            ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
            – hoijui
            Feb 27 at 14:04





            ouhh i did not know about begin at all, thanks! maybe also add a link to the docu: fishshell.com/docs/current/commands.html#begin PS: nice answer!
            – hoijui
            Feb 27 at 14:04













            up vote
            0
            down vote













            Alternative solution: outsource part of the conditional chain into a function



            like so:



            #!/usr/bin/fish

            function _my_and_checker
            return $argv[1]; and argv[2]
            end
            function _my_or_checker
            return $argv[1]; or argv[2]
            end

            if _my_and_checker false true ; or true
            echo "true"
            else
            echo "false"
            end

            if false; and _my_or_checker true true
            echo "true"
            else
            echo "false"
            end


            This makes most sense if the conditions themselves are complex commands.






            share|improve this answer
























              up vote
              0
              down vote













              Alternative solution: outsource part of the conditional chain into a function



              like so:



              #!/usr/bin/fish

              function _my_and_checker
              return $argv[1]; and argv[2]
              end
              function _my_or_checker
              return $argv[1]; or argv[2]
              end

              if _my_and_checker false true ; or true
              echo "true"
              else
              echo "false"
              end

              if false; and _my_or_checker true true
              echo "true"
              else
              echo "false"
              end


              This makes most sense if the conditions themselves are complex commands.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Alternative solution: outsource part of the conditional chain into a function



                like so:



                #!/usr/bin/fish

                function _my_and_checker
                return $argv[1]; and argv[2]
                end
                function _my_or_checker
                return $argv[1]; or argv[2]
                end

                if _my_and_checker false true ; or true
                echo "true"
                else
                echo "false"
                end

                if false; and _my_or_checker true true
                echo "true"
                else
                echo "false"
                end


                This makes most sense if the conditions themselves are complex commands.






                share|improve this answer












                Alternative solution: outsource part of the conditional chain into a function



                like so:



                #!/usr/bin/fish

                function _my_and_checker
                return $argv[1]; and argv[2]
                end
                function _my_or_checker
                return $argv[1]; or argv[2]
                end

                if _my_and_checker false true ; or true
                echo "true"
                else
                echo "false"
                end

                if false; and _my_or_checker true true
                echo "true"
                else
                echo "false"
                end


                This makes most sense if the conditions themselves are complex commands.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 27 at 16:08









                hoijui

                1217




                1217






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426928%2fhow-to-group-multiple-conditions-in-an-if-statement-in-fish%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)