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

32,164

Solution 1

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).

Solution 2

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

Share:
32,164
Albert
Author by

Albert

I am postgraduate of RWTH Aachen, Germany and received a M.S. Math and a M.S. CompSci. My main interests are Machine Learning, Neural Networks, Artificial Intelligence, Logic, Automata Theory and Programming Languages. And I'm an enthusiastic hobby programmer with a wide range of side projects, mostly in C++ and Python. Homepage GitHub SourceForge HackerNewsers profile page MetaOptimize Q+A

Updated on September 18, 2022

Comments

  • Albert
    Albert almost 2 years

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

    Is there any equivalent in Fish?

  • aboy021
    aboy021 over 8 years
    There's an open github issue to add support for this syntax: && doesn't work · Issue #150 · fish-shell/fish-shell
  • Petr Peller
    Petr Peller about 8 years
    This allows using a more familiar syntax: is very subjective
  • Elliott Beach
    Elliott Beach almost 7 years
    ;and is less readable than && as the semicolon suggests a logically disjoint operation. It's visually jarring.
  • Dennis
    Dennis almost 7 years
    @Elliott I agree, but Fish doesn't give you a choice.
  • balupton
    balupton over 6 years
    do note though that in fish and bourne shells, AND and OR operators have the same order, unlike C based languages: unix.stackexchange.com/a/88851/50703
  • math2001
    math2001 almost 6 years
    @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).
  • Admin
    Admin about 2 years
    You can't do 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.