Why can't I kill the shell using the PID obtained from the $$ special variable?

5,640

Solution 1

You can, but the shell tries hard not to die unless it's absolutely sure that's what is required.

SIGHUP works (as does SIGKILL), and you can try this -

kill -HUP $$

(If you prefer numeric signal identifiers the HUP can be replaced with 1.)

The reason that SIGHUP works is that this is the signal that would have been sent when a serial line connection over a modem was terminated - for instance if the phone line was hung up.

Solution 2

Check the bash manual:

When bash is interactive, in the absence of any traps, it ignores SIGTERM

https://www.gnu.org/software/bash/manual/bashref.html#Signals

Check the kill help text (help kill at a bash prompt):

If neither SIGSPEC nor SIGNUM is present, then SIGTERM is assumed.

https://www.gnu.org/software/bash/manual/bashref.html#index-kill

Share:
5,640

Related videos on Youtube

Abdul Al Hazred
Author by

Abdul Al Hazred

Updated on September 18, 2022

Comments

  • Abdul Al Hazred
    Abdul Al Hazred almost 2 years

    After I have learned that the $$ special variable holds the process number of the shell, I tried too kill the shell by simply: kill <pid obtained by echo $$>, however this did not work, I also tried some variations, but them also had no effect. enter image description here

    • Leiaz
      Leiaz over 9 years
      xargs is totally overkill here. Just use kill $$ (although it won't work, as explained in link above). And you should copy paste the text instead of posting a screenshot.
    • Abdul Al Hazred
      Abdul Al Hazred over 9 years
      I wanted to copy paste first, but once I tried , I got a total unformatted character mess.
    • jasonwryan
      jasonwryan over 9 years
      As with every one of your other questions, this one exhibits no effort to search: either for previous questions or how to format text on the site. Please stop being such a help vampire.