How can I exit from command prompt from shell file?

shell file contains :

if(); then
// some unix code here
// want to exit from cmd prompt or Kill current cmd prompt

can any knows @ it?

  • 1
    At least in GNU bash, have you considered kill $PPID? That will kill the parent process of your (script) process, because $PPID is the parent process ID. That said, this sounds like an awfully bad idea. At least I would not appreciate having my shell yanked out from under my feet, and even less so if I'm logged in e.g. through SSH to a remote host (perhaps having launched a few X applications through it, too). – user Apr 18 '13 at 6:21
  • It can be helpful for me.. do you know how can i ask question to whether 'you want to exit from command promt or not? in YES or NO' in shell file. so that i can use your idea to kill parent process. can you tell me the code please ? – Bhushan J Apr 18 '13 at 6:26

Like Chris Down, I'm going to assume that you are using GNU bash. Other shells may be different, but the general idea should be the same.

In bash, the environment variable $$ contains the process ID of the currently running process, and $PPID contains the process ID of the parent of the running process. As Chris exemplified, you can go even further up, but /proc is Linux-specific.

You can ask the user whether to exit using a construct like the following.

echo "Do you want to exit? (type YES or NO, then Enter)"
read answer
if test "${answer}" = "YES"; then
    # insert process killing magic
# we only get here if the user did not answer "YES"

If it is available, you can use GNU dialog to make the question a little prettier. In that case, replace the entire snippet above with something like the following.

if dialog --yesno "Do you want to exit?" 10 50; then
    # insert process killing magic
# we only get here if the user did not answer "YES"

The numbers 10 50 specify the size of the dialog box (rows and columns, respectively).

The process killing magic could be something as simple as kill $PPID.


I assume that you mean you want to kill the interactive shell that the script was forked from. I also assume that that shell is bash.

If you are sourcing the script in that shell, just use exit. Otherwise, send SIGABRT (or another signal that will cause bash to exit) to the parent pid (the interactive shell) by using something like this:

kill -ABRT "$PPID"

Or, in shells without $PPID (this is Linux specific):

read -r _ _ _ ppid _ < "/proc/$$/stat"
kill -ABRT "$ppid"
  • @BhushanJ If you found an answer helpful, please upvote it by clicking the up arrow in the left hand column. – user Apr 18 '13 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.