0

I'm running Terminal Version 2.6.1 on Mac. I'm writing a bash script, and I want to exit the script if a certain condition is met.

I do that with this command:

exit 1

after doing that, the script exits and I see this on the terminal:

Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

After that, my terminal is frozen and I can't do anything with it anymore.

Is there a way to negate having to open up a new terminal each time the script quits?


I am running the script from my terminal like this:

. script.sh
10
  • 1
    Huh. My MacOS system doesn't have a quit command at all. I'd be curious as to what type quit returns for you. (As the existing answer says, the proper way to cause the shell running a script to quit is exit). Commented Sep 9, 2016 at 21:20
  • BTW, how are you running your script? If you do it with . yourscript or source yourscript, then the script is being run by your main interpreter, so exiting it exits the terminal. Commented Sep 9, 2016 at 21:22
  • Whooops! Yup I meant exit but I wrote quit Commented Sep 9, 2016 at 21:22
  • exec yourscript will similarly replace your interactive shell with the one running the script, so when the script exits you have nothing left. Commented Sep 9, 2016 at 21:22
  • 1
    The question is why are you running the script as . script.sh? There are reasons to do that, but it's atypical, and is generally the wrong thing to do. Commented Sep 9, 2016 at 21:32

1 Answer 1

4

Don't use

. script.sh

to run the script. This runs the script in the same interactive shell that you're using in the Terminal session, and when the script uses exit, that exits the interactive shell as well.

Start the script with this line, which tells the OS to use bash to execute the script:

#!/bin/bash

Give it execute permission with:

chmod 755 script.sh

and then run it with

./script.sh

This runs the script in its own process.

Sign up to request clarification or add additional context in comments.

16 Comments

. script.sh is one way this could happen (like its synonym source script.sh); exec ./script.sh is another.
That's why I didn't recommend that. He should just do what I said in the answer.
Sorry? I was offering a second/alternate theory of what the mistake could be, not a second proposed fix.
@etayluz, if you do that, someone has to know which interpreter your script needs to be run with before they can use the script. That also means that if you rewrite the script to be written in a different language, every single other script that calls/uses it needs to be rewritten.
@etayluz sourcing a script runs it in the same shell process, executing the script starts a child process to run it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.