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

2

28

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?

Tobacconist answered 9/11, 2013 at 12:38 Comment(1)
Note that set -e is a bad idea in bash, where consistent use of || return or || exit (like the fish or return suggested by Fractale below) is the more reliable practice.Amadavat
M
34

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
Multistage answered 9/11, 2013 at 22:3 Comment(0)
Z
0

another popular option is:

cp file1 file2; or return
rm file1; or return
echo File moved; or return
Zoa answered 14/5 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.