1

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
Community
  • 1
  • 1
Var87
  • 519
  • 1
  • 4
  • 17
  • 2
    Does your script not exit or does the terminal not quit? If the script exits and the terminal doesn't quit when you run the script from a shell prompt that's because it is only exiting the script and not the running shell session. Source the script (`. script.sh`) instead of running it (`./script.sh`) and you will exit from the running shell. – Etan Reisner Jan 01 '15 at 12:56

1 Answers1

2

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.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148