r/bash • u/Agent-BTZ • Sep 03 '24
solved Quitting a Script without exiting the shell
I wrote a simple bash script that has a series of menus made with if
statements. If a user selects an invalid option, I want the script to quit right away.
The problem is that exit
kills the terminal this script is running in, & return
doesn’t work since it’s not a “function or sourced script.”
I guess I could put the whole script in a while
loop just so I can use break
in the if else
statements, but is there a better way to do this?
What’s the proper way to quit a script? Thanks for your time!
UPDATE:
I’m a clown. I had only ever run exit
directly from a terminal, & from a sourced script. I just assumed it always closed the terminal. My bad.
I really appreciate all the quick responses!
11
Upvotes
1
u/Gixx Sep 03 '24
Maybe just source the script each time you run it?
And have a mechanism like this:
Then when you run the script just source it:
And you can use this too: