I'm writing a shell script that needs to do a bunch of commands and every command depends on every previous command. If any command fails the entire script should fail and I call an exit function. I could check the exit code of each command but I am wondering if there is mode I can enable or a way to get bash to do that automatically.
For example, take these commands:
cd foo || myfunc
rm a || myfunc
cd bar || myfunc
rm b || myfunc
Is there a way where I can somehow signal to the shell before executing these commands that it should call myfunc if any of them fail, so that instead I can write something cleaner like:
cd foo
rm a
cd bar
rm b