user1494787
user1494787

Reputation: 303

Fish Pipeline Status

In fish:

if false | true | true
  echo "Fish thinks OK because of last status"
else
  # But I...
  echo "Need the entire pipeline to be true"
end

Bash has $PIPESTATUS. How does one test the integrity of a pipeline in Fish?

To clarify... I'm using true and false in the example pipeline as an example of a pipeline which last component succeeds. It's not meant to be a boolean statement. Normally, if any component of a pipeline fails, one would consider the pipeline as having failed.

Upvotes: 3

Views: 699

Answers (2)

mattmc3
mattmc3

Reputation: 18325

This question is really old and didn't have an accurate answer for modern Fish. The feature request faho mentioned (#2039) was implemented in 2019. You can now check pipestatus like other shells:

true | false | true | true
if string match -qr '[^0]' $pipestatus
    echo "handle error"
end

If you are running multiple test calls against $pipestatus, remember it's volatile and needs to be saved off into its own variable so you don't nuke it amidst multiple calls to test:

true | false
set --local last_pipestatus $pipestatus
if test $last_pipestatus[1] -ne 0 || test $last_pipestatus[2] -ne 0
    echo "handle error"
end

Remember, it's a weird Schrödinger's variable. It changes every time you peek in the box. Every. Single. Command. Even if you simply echo $pipestatus, you've changed its value. That's not unique to Fish - Bash behaves the same:

$ # Bash pipestatus is also volatile
$ true | false | true | true
$ # This line shows and then changes $PIPESTATUS
$ echo ${PIPESTATUS[@]}
0 1 0 0
$ # And now it's gone. It shows results from the prior echo now.
$ echo ${PIPESTATUS[@]}
0

Upvotes: 0

faho
faho

Reputation: 15924

It's not currently possible (other than ugly workarounds where you construct the pipe manually via set var (echo $initialinput | firstcommand); and set var (echo $var | secondcommand); and...j

This is tracked as fish bug #2039.

Upvotes: 2

Related Questions