4

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
hoijui
  • 731

2 Answers2

7

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
hoijui
  • 731
Stefan M
  • 1,616
1

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.

hoijui
  • 731