I'm facing an issue with SSH on a server. Running individual scripts (source s01.sh, etc.) works fine, with errors displayed in the console. However, when using a master script to source these scripts, the SSH connection unexpectedly terminates upon encountering an error. The master script master.sh is as follows:

set -e

source s01.sh
source s02.sh
Rscript s03.R
source s04.sh

I source the master script in interactive SSH session using

source master.sh

With set -e, I expect the script to stop at errors, but instead, it's causing the entire SSH session to drop. This only happens with the combined script, not with individual sourcing. Why does this occur, and how can I keep the SSH session from closing on script errors?

I am aware this might involve SSH server configurations, but I'm unsure which settings to investigate.

  • PLease edit your question and explain how you are running the combined script. Is it ssh server script.sh or do you first open an interactive session and then script.sh? or is it bash script.sh? We need the details to be able to reproduce this.
    – terdon
    Nov 26, 2023 at 11:35
  • @terdon Done! Thanks for pointing out.
    – jay.sf
    Nov 26, 2023 at 12:06
  • Ah, and do you need to source the master.sh? Looks like it would be fine to just execute the master, in which case you wouldn't have the problem.
    – terdon
    Nov 26, 2023 at 12:11
  • @terdon Actually I already made master.sh executable but forgot about not needing source to execute it. Thanks for clarifying this.
    – jay.sf
    Nov 26, 2023 at 13:03

1 Answer 1


The reason this is happening is because you are sourcing the master script. I can reproduce the behavior you describe using these two toy examples:

terdon@oregano ~ $ cat ~/scripts/foo.sh
set -e
source /home/terdon/scripts/bar.sh
echo "done!"

terdon@oregano ~ $ cat ~/scripts/bar.sh

echo "start"

Now, if I open a new terminal and run . ~/scripts/foo.sh (note that source is a non-portable bash alias to the standard . command, so . foo is the same as source foo), the terminal I opened just exits. And if I try it in an ssh session, the ssh session exits. Why? Because sourcing means executing the commands and applying them to the current session. So since one of the scripts causes an error (the command false just exits with 1), the set -e causes the session to exit. It doesn't just kill the script because you have told it to apply the commands to the current shell session by sourcing instead of executing.

However, from your example, it looks like you don't need to source the master script, all you want is to source the other scripts which presumably are setting up the environment needed by your R script. So just execute instead of sourcing and you should be fine:


Or, if you haven't made it executable:

bash /path/to/master.sh

See this post for more on sourcing vs executing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .