I’m writing a bash script that leverages getopt to process a number of options provided after the script name.
I’m accessing my shell through puTTy.
I invoke the script with . scriptname.sh -o –options (is there a better way to execute a bash script?)
I’d like to be able to abort / halt the script, but whenever I use exit it not only exits the script, it also closes the terminal/puTTy window.
…which kinda defeats the purpose because what I’m trying to do is set up some kind of –help text output that displays the text but doesn’t execute the rest of the script.
What’s the deal with exit and what should I use to halt/abort the script without closing any parent console window?
Using
exitis correct, you’re just not executing your script correctly.Using
.is the same as using thesourcecommand (help sourcefor usage) and is not a good idea if you really wanted to execute the script.Instead
chmod a+x scriptname.shand then use./scriptname.shto invoke it.The difference is that
sourcetreats the script as if it had been typed in by you; it runs in the samebashsession that you’re running interactively. This means that any variables it changes remain changed after it finishes. It also means that if you tell the script toexit, it will correctly terminate your current bash session.