back to listing index

How to properly restore settings when running a script using "source"?

[web search]
Original source (superuser.com)
Tags: bash shell-scripting
Clipped on: 2018-01-03

Welcome back! If you found this question useful,
don't forget to vote both the question and the answers up.
close this message

I'm writing a script that needs to be executed using source, because its task is to modify the environment. During the script, I want to use set -e so that any error stops the script. The script looks like this:

#!/bin/bash
set -e
# do something
cd somewhere
source anotherScript

The problem is that set -e naturally remains in effect after the script finishes. How can I ensure that the altered set option is properly restored to its prevous value when the script stops (in any way - either by completing successfully or on an error)?