Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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). – Michael Kjörling 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
up vote 0 down vote accepted

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.

share|improve this answer
Thank you very much Michael – Bhushan J Apr 18 '13 at 6:49

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"
share|improve this answer
Thanks fro the help – Bhushan J Apr 18 '13 at 6:49
@BhushanJ If you found an answer helpful, please upvote it by clicking the up arrow in the left hand column. – Michael Kjörling Apr 18 '13 at 9:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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