I'm sourcing a bash script in the terminal, so exiting on error with
set -o errexit
kills my terminal, which is EXTREMELY ANNOYING, because I have to close the terminal, open another one, and reset some variables.
So far, using
command || return
lines, in the script, is doing exactly what I want
set -o errexit
to do... But I want it done for the entire script; not just one line/command
I have a file full of commands for setting up a site, and I'd rather not do command || return
for every single line in the file
Is 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.sh source $create_path blah
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. Ugghhh
here's some examples:
#!/bin/bash create_path=~/Desktop/site_builder/create.sh source $create_path blah
#!/bin/bash set -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 error
in the terminal:
$ bash super.sh
output as expected:
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:
my-mac$ source $create_path blah
in create.sh I still have
set -o errexit
Here's the output on the terminal
sed: 1: "s/@@SITE_NAME@@/blah": unterminated substitute in regular expression Saving 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+D
If 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.