In Bash/ZSH and other shells, I am used to using && and ||.
Is there any equivalent in Fish?
In Bash/ZSH and other shells, I am used to using && and ||.
Is there any equivalent in Fish?
Fish doesn't have a special syntax for a logical AND (&&) or a logical OR (||).
Instead, you can use the commands and and or, which verify the previous command's exit status and act accordingly:
command1
and command2
command1
or command2
Furthermore – just like in bash – you can use a semicolon ; to execute two commands one after the other:
command1 ; command2
This allows using a more familiar syntax:
command1 ;and command2
command1 ;or command2
See http://fishshell.com/docs/current/tutorial.html#tut_combiners
;and is less readable than && as the semicolon suggests a logically disjoint operation. It's visually jarring.
– Elliott Beach
Jul 30 '17 at 20:08
command1 and command2 though, command 2 will never be run because "and" will be considered as argument 1 for command1, and command2 will be considered as argument 2 for command1. For example if true and true; echo yes; end won't work because fish will think you're passing the arguments "and true" to the command true. if true; and true; echo yes; end is the correct way.
– Shardj
Jun 01 '22 at 14:17
The logical operators you're used to, are supported since fish 3.0.0, released on 2018-12-28.
From the v3 release notes:
- fish now supports
&&(likeand),||(likeor), and!(likenot), for better migration from POSIX-compliant shells (#4620).