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 Answers
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.
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.
-
1
-
This "answer" is correct, but mostly for the scenario when
exit
is executed. Dec 18, 2021 at 22:39
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.
rc.local
, which are run withsh -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.