Var87
Var87

Reputation: 569

OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?

This bash script in osx does not exit the script. I have changed the settings (as suggested here so that writing exit in terminal closes the window, and I've checked that writing exit does close the terminal window if I write it in the terminal, What could be the reason for this? I restarted terminal and my mac after changing the settings to see if that solved the problem. But exit in at location(s) nr 1 or at the end of the script does not work.

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH
read -p "What do you want to do?"
    if test "$pass" = "f"
    then
        sh ~/f.sh
        exit # nr 1
    fi
     if test "$pass" = "s"
     then
         sh ~/s.sh
         exit # nr 1
     fi
     exit # nr 2

Upvotes: 1

Views: 953

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

Assuming you mean that when you do:

$ exit

your terminal exits.

But when you run

$ ./script.sh

your terminal doesn't exit.

The answer is that your script is being run in its own shell process and so when it exits it isn't exiting from the shell that is running in the terminal.

If you use

$ . script.sh

then you will run the script in the current shell process so exit will exit the running shell and cause the terminal to exit.

Upvotes: 2

Related Questions