c4k
c4k

Reputation: 4426

Exit a shell script properly in Mac OS X

I have a shell script that create for me a project with the full structure, git repository etc... The problem is that I can't use the terminal after the execution of the test. Here is a sample of my script :

read -p "Have you created the remote repository : $repo.git ? [y/n] " ok
if [ $ok != "y" ]; then
    echo "You must create the remote repository before."
    exit 0;
fi

git init
# Rest of the script...

When I type "n" as an answer, the terminal displays this (for non-french people, "Opération terminée" means "End of the operation") :

You must create the remote repository before.
logout

[Opération terminée]

And I can't use it anymore. The only way to use it again is to close the tab and open another one. Maybe the problem is in the exit 0 ? How to exit the script properly ?

Thanks.

Upvotes: 1

Views: 14120

Answers (2)

jcragun
jcragun

Reputation: 2198

Since this runs from the bash_profile, exit 0 actually closes the shell based on its context. Normally, a script is what's calling the function so it exits not the shell. For example, if you run echo "hello world"; exit 0; directly in the shell then it closes the shell because that's the context where exit occurs.

As suggested by @mklement0, replace exit 0 with return.

Upvotes: 9

l0b0
l0b0

Reputation: 58768

Are you backgrounding the function with & at the end of line somewhere? That would work like sleep 1 & and only print that it's done once you press Enter. The output when the process ends depends on the shell, but in Bash 4.2 it looks like this:

$ sleep 1 &
[1] 11166
$ # Just press Enter after more than a second
[1]+  Done                    sleep 1

Upvotes: 0

Related Questions