1

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.

3
  • How are you invoking the script? Is it in a file called script.sh with 0755 perms and #!/bin/sh at the top and you are calling it with ./script.sh or /path/to/script.sh? Commented Apr 25, 2014 at 15:18
  • No, I have a function init-project in my .bash_profile and I call this function Commented Apr 25, 2014 at 15:22
  • It needs to be in a shell script which accepts arguments, not interrogates the user. This will then run in a sub-shell and all will be well. Commented Apr 25, 2014 at 16:34

2 Answers 2

9

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.

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

1 Comment

Perfect, replacing exit by return fixed it ! Please add it as an answer so that I can accept it.
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.