0

How can I prevent a bash script from closing, so block itself from closing.

Is there a command that executes a command if there is no action?

  • Welcome to [unix.se] :-) while : ; do done; anywhere inside your bash script or at the end will prevent a bash script from closing but that'll be of very little use. Can you [edit] your question and provide the current bash script that you're trying to prevent to close? – Fabby Apr 12 '19 at 20:54
  • 2
    Despite deleting and recreating this question, we still aren't clear on what you are asking. A shell script typically runs through its commands and then exits (closes, ends, etc.) Some programs, which may be bash scripts, can remain open and behave interactively with a users input or other externals. But if a script comes to the end, it finishes, closes, exits. Your question comes across like "how can I prevent the Night from ending?" Perhaps reconsider your problem and edit the question to ask a different aspect of the problem? – 0xSheepdog Apr 12 '19 at 20:55
  • I think this is what he's looking for: https://askubuntu.com/questions/20330/how-to-run-a-script-without-closing-the-terminal/1209836#1209836 – Gabriel Staples Feb 12 '20 at 02:05
  • Just like me, maybe you also find useful this article explaining the basics of the best ways how to properly end a shell script: https://web.archive.org/web/20230604212552/https://www.baeldung.com/linux/safely-exit-scripts – Balu Ertl Oct 05 '23 at 00:32

2 Answers2

6

From your other (now deleted) question, it seems you want to run

xterm -e your-script

And the terminal emulator window not to go away after the script finishes.

For that, you could add a command that sleeps forever at the end of your script, or in an EXIT trap. See How to do nothing forever in an elegant way? or Is there a Linux command that does nothing, but never exits? for some options.

trap 'sleep infinity' EXIT

Would cause the shell to run sleep infinity upon exit, and so never exit. With those sleep implementations that don't support infinity, replace with a large number, like sleep 2147483647 (the largest 32 bit signed integer which should be safe on most systems and is about 68 years).

With xterm, you can also use its -hold option which is designed for that.

0

Another way of blocking can be using a user input to block. Try using read varname at the end program so that it will keep waiting for user input and will not exit.