mdesantis
mdesantis

Reputation: 8517

Fish shell: how to exit on error (bash set -e)

On bash you can use set -e inside a script in order to exit on error:

set -e
cd unexisting-folder
echo "this line will not be printed"

But on fish shell set -e is used to erase variables:

set FOO bar
set -e FOO
echo {$FOO} # prints newline

What is the equivalent of Bash set -e on Fish?

Upvotes: 31

Views: 6336

Answers (3)

Tobia
Tobia

Reputation: 18811

Yet another option is to prepend a custom function to the commands you wish to check:

function try
  if ! $argv
    echo "ERROR ($argv)"
    exit 1
  end
end

try cd unexisting-folder
echo "this line will not be printed"

Upvotes: 0

Fractale
Fractale

Reputation: 1654

another popular option is:

cp file1 file2; or return
rm file1; or return
echo File moved; or return

Upvotes: 0

ridiculous_fish
ridiculous_fish

Reputation: 18561

There's no equivalent of this in fish. https://github.com/fish-shell/fish-shell/issues/805 spend a little time discussing what a fishy version of this might look like.

If the script is short, prefixing each line with and might not be too bad:

cp file1 file2
and rm file1
and echo File moved

Upvotes: 35

Related Questions