I'm sourcing a bash script in the terminal, so exiting on error withset -o errexitkills my terminal, which is EXTREMELY ANNOYING, because I have to close the terminal, open another one, and reset some variables.So far, usingcommand || returnlines, in the script, is doing exactly what I want set -o errexitto do... But I want it done for the entire script; not just one line/commandI have a file full of commands for setting up a site, and I'd rather not do command || returnfor every single line in the fileIs there another set option, or something else that will just return instead of exiting the terminal?-- Just for clarity, I'd like to kill the script, and leave the terminal in the same state that pressing ctrl+C to kill a service running in the terminal would. command || return does that. But I don't want to tack on || return to every line in the file. So I'm looking for something  similar to set -o errexit, that doesn't cause the terminal to shut down--- Note: Creating a dumb script with two lines in it (super.sh):create_path=~/Desktop/site_builder/create.shsource $create_path blahAnd placing set -o errexit at the top of create.sh, works exactly as I expect it. However, it's really stupid to have to create a file with two lines in it, just to call another bash script, instead of just calling it from the terminal. Ugghhhhere's some examples:in super.sh#!/bin/bashcreate_path=~/Desktop/site_builder/create.shsource $create_path blahin create.sh#!/bin/bashset -o errexit#line below this is a line that fails and will cause the script to stop and return to the terminal as expected sed s/@@SITE_NAME@@/$dirname ~/Desktop/site_builder/template_files/base.html > ~/Desktop/$dirname/templates/base.html # a line with a stupid errorin the terminal: $ bash super.shoutput as expected:my-mac$This works. What an annoying solution.I want , ideally, to execute what's in the stupid super.sh file from the terminal, not the super.sh file :D, without having the terminal shut down on me. This is what happens with what I'm trying to do:terminal command:Jills-MBP:~ jillr$ source $create_path blahin create.sh I still have set -o errexitHere's the output on the terminal    sed: 1: s/@@SITE_NAME@@/blah: unterminated substitute in regular expressionSaving session......copying shared history......saving history...truncating history files......completed.[Process completed]And then the terminal is frozen. Ctrl+C doesn't work, neither does Ctrl+DIf instead of set -o errexit , if I just use command || return statements everywhere in the create.sh file, then I get exactly what I want , while executing the lines in supser.sh directly on the terminal (instead of calling super.sh from the terminal). But that's not a practical solution either.Note: I liked @terdon 's answer about just spawning a child shell so I ended up just spawning a sub shell via the script instead of the terminal, as he showed in his answer using the braces ( ), around the entire script.. His answer works too.
sourcing a Bash script - Return on Error, instead of Exit?
bash;shell script
As a simple workaround, you could run a shell in your current shell and source there. Something like:Open a new terminal and set everything up the way you want it. You mentioned some environment variables and the like. Set them up here. In that terminal, start a new shell. For instance, bash. Do your thing. Source your script. If it exits, you're just thrown into the first shell and everything is still set up. Just run bash again and you're back in business. To illustrate, I created this script which will fail if you try to source it:$ cat /home/terdon/scripts/bar.shset -o errexitvar='bar'Let's see what happens if I start a nested shell session and then source it (note that I am using the portable name for the source command, .; source is a bashism):parent-shell $ bash      ## start a new shellchild-shell $ . ~/scripts/bar.shbash: /home/terdon/scripts/bar.sh: line 2: unexpected EOF while looking for matching `''parent-shell $ As you can see, the syntax error caused the sourced script to exit which, in turn caused my shell session to exit but since it was a nested session, that just landed me back at the original, parent shell with all the variables still set up. Now, just run a new shell again and you can go back to sourcing your script. 