Search code examples
shellfish

How to terminate execution of a fish script from a sourced file?


The exit command when executed from a file being sourced doesn't terminate the execution of the program where it was being sourced, how to do this? Consider this files for a clearer explanation:

a.fish:

source b.fish
echo "This should never run!"

b.fish:

echo "Failing now"
exit 1

This will result in this (undesired output):

Failing now
This should never run!

And the exit status is 0! Is there a solution for B to terminate execution of A as if exit was written in A itself?


Solution

  • It's perfectly working the way you want with bash and zsh. Still, I found a solution for fish:

    source b.fish; or exit 1
    

    This will exit a.fish if b.fish exited with exit 1, and will continue otherwise.