Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

This can be considered a follow-up to: How to save shell history from emacs and use it in terminal?

I'm trying to force bash to exit with the following in my .emacs file, but it doesn't work, even if I include an embedded ^M character after the word "exit". Can this be made to work?

(defun bye ()
  "Leaving emacs now"
  (goto-char (point-max))
  (insert-string "exit"))
(add-hook 'kill-emacs-hook 'bye)
share|improve this question

Part of the reason why what you're doing won't work is because (shell) actually launches a new inferior shell, which may or may not then be the target for (insert-string); in any case, the most it'd accomplish is to exit the inferior shell. The Emacs wiki has an unanswered question on the subject, which is relatively rare over there on any subject; between that, and the general disinterest evinced by non-batch-mode Emacs in communicating with its parent process by any means, I tend to doubt what you're attempting is possible.

That doesn't, though, mean that you can't get the desired result -- after all, you need not have Emacs communicate with its parent process; if you replace your shell process with Emacs, then whatever terminal you're using will exit at the same time Emacs does, in the same fashion as any terminal when the process attached to its pty finishes.

The method will vary depending on your shell, but in Bash, zsh, and probably tcsh as well (assuming anyone still uses it), you need simply preface your Emacs invocation with exec -- if you usually say emacs -nw, instead say exec emacs -nw, &c.

This assumes you're invoking Emacs directly from the shell, as seems likely. If you use (for example) a shell script to invoke Emacs, this won't work, because exec will replace the shell process executing the script, rather than the one bound to your pty; in that case, just exec the shell script instead, and you should still get the result you're after unless your script does something really weird, in which case you should update your question to include it and someone will probably be able to figure something out that'll work.

share|improve this answer
Actually it is the inferior shell I am trying to make exit, and (shell) only launches a new inferior shell if there is no existing buffer named "*shell*". I pretty much always have a buffer named "*shell*" so I believe that if I could get bash to process the "exit" command I am inserting, it would work. – wytten Aug 26 '13 at 17:21
I went back and followed the suggestion you made in the previous question about reading the bash FAQ, and it looks like adding PROMPT_COMMAND="history -a" to my .bashrc is a solution that works, thanks! – wytten Aug 26 '13 at 17:29
@wytten Fair enough; glad to help. – Aaron Miller Aug 26 '13 at 17:52

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.