2
echo $$ 

It tells the PID of the shell. So, if I kill this shell using

kill 123 #suppose 123 is PID of shell

Nothing happens actually, I tried it. I again ran

echo $$

and the shell was running under same PID. Why the shell is not stopped ? What may happen if the shell is killed ?

  • 1
    You are not killing the shell, you'd be killing the "terminal window." You can kill terminal A from terminal B, though. – M. Becerra Mar 8 '17 at 18:50
  • Can a terminal kill itself by this way ? – luv.preet Mar 8 '17 at 19:03
  • Why not just press Ctrl+D (which sends EOF)? – wjandrea Mar 8 '17 at 19:51
  • 1
    @m.becerra pid of window is different from what shell pid is, which is what $$ is supposed to show – Sergiy Kolodyazhnyy Mar 8 '17 at 20:35
  • This works: kill -9 $$ – Siguza Mar 8 '17 at 21:00
3

The main reason is because shells are built to trap the TERM signal. This behavior is documented. To quote bash 4.3 manual:

SIGNALS

When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught and handled (so that the wait builtin is interruptible). In all cases, bash ignores SIGQUIT. If job control is in effect, bash ignores SIGTTIN, SIGTTOU, and SIGTSTP.

From man kill:

The default signal for kill is TERM.

Now if you were to specify kill -KILL $$, what would indeed kill your shell, and once the controlling shell exits - that would close the terminal. The options -9, -KILL and -SIGKILL are all aliases for the same kill signal, which is why any of these 3 work, and is one of the aliases that Siguza pointed out in the comments.

Contrasting to what M. Becerra said, this has nothing to do with window itself, as window PID is always different from shell's:

# Shell PID
$ cat /proc/self/status  | grep '^Pid:'                                                                                  
Pid:    7058
$ echo $$                                                                                                                
14709
# GUI window PID
$ xdotool getactivewindow getwindowpid 
2774
| improve this answer | |

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.