6

I wrote my own shell script, and I made it executable with chmod +x command. But when I click to shell file and want to run it in terminal, it suddenly opens and then closes in a second. What is the problem?

3
  • 2
    When scrpits have errors they exit. So unless you list the contents of the script its a guessing game
    – Meer Borg
    Apr 23, 2013 at 12:18
  • no, script hasn't any error. I just want to simply run it in Terminal with clicking to the current file. Apr 23, 2013 at 13:07
  • Also, it's not true that scripts always exit when they have errors. Scripts such as rc.local, which are run with sh -e, will do (essentially) this. But most scripts don't run that way. Of course many scripts are deliberately written to exit under a variety of specific error conditions, but the author of the script would know about this if they had deliberately written their script that way. Oct 9, 2014 at 23:03

3 Answers 3

10

It also closes automatically once it has run your command.
Add /bin/bash at the end of your script to keep the terminal open.

You can also add a && sleep 10 to keep the terminal open 10 seconds and see what's going on.

Alternatively, add another line containing read at the end of the script to keep the terminal open until you press Enter.

1
  • or add the sleep 10 to the exit point (end?) of the script.
    – Elder Geek
    Oct 10, 2014 at 2:43
8

I have faced a similar situation. I was executing my script as

. helper.sh

Instead try executing it as

./helper.sh

This worked for me.

2
  • 1
    FYI: . script.sh means the same as source script.sh
    – Iacchus
    Nov 11, 2021 at 14:41
  • This "answer" is correct, but mostly for the scenario when exit is executed. Dec 18, 2021 at 22:39
2

Try commenting out all your "exit" commands (if any) in your script by placing # in front of them and give it a go. Perhaps you are executing "exit" in your shell that closes the terminal session.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .