chain Fish commands via `&&` or `||`

96

30

In Bash/ZSH and other shells, I am used to using && and ||.

Is there any equivalent in Fish?

Albert

Posted 2012-07-10T03:01:59.097

Reputation: 5 059

This syntax now supported on the master branch and will be released in Fish 3.0 (https://github.com/fish-shell/fish-shell/commit/014b91488db29480160284adfd1cddf286d2888a)

– Warlike Chimpanzee – 2018-03-29T01:31:20.513

Answers

12

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 && (like and), || (like or), and ! (like not), for better migration from POSIX-compliant shells (#4620).

Dennis

Posted 2012-07-10T03:01:59.097

Reputation: 352

134

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

Dennis

Posted 2012-07-10T03:01:59.097

Reputation: 42 934

4

There's an open github issue to add support for this syntax: && doesn't work · Issue #150 · fish-shell/fish-shell

– aboy021 – 2016-02-26T00:41:23.427

20This allows using a more familiar syntax: is very subjective – Petr Peller – 2016-06-02T10:46:24.880

1;and is less readable than && as the semicolon suggests a logically disjoint operation. It's visually jarring. – Warlike Chimpanzee – 2017-07-30T20:08:20.217

@Elliott I agree, but Fish doesn't give you a choice. – Dennis – 2017-07-30T20:11:59.893

2

do note though that in fish and bourne shells, AND and OR operators have the same order, unlike C based languages: https://unix.stackexchange.com/a/88851/50703

– balupton – 2018-01-11T08:31:38.760

@dennis which is a good thing: there's only one option, and you don't have to choose, it's already chosen for you. Plus I think that using a "native" solution instead of implementing a new syntax if far better, even though it might now look as good (and, as petr said, it's very subjective). – math2001 – 2018-07-09T03:06:44.597