The following program is from https://www.cs.bu.edu/teaching/tool/emacs/programming/

#include <iostream>

int main()
  int howmany;

  std::cout << "Enter a number: ";
  std::cin >> howmany;
  std::cout << "howmany=" << howmany << std::endl;

  return 0;

It compiled and ran in Emacs shell:

SPC c C  (compile)
SPC !    (shell-command)
Shell command: ./total

The program did not stop and prompt for input. The program just blows by the "std::cin >> howmany;" and assigns 0 to howmany. I did not enter any input. For one second the mini buffer said:

Enter a number: 0

The *Messages* buffer says:

user-error: Minibuffer window is not active
Enter a number: howmany=0

The executable works fine when run from Bash shell:

$ ./total
Enter a number: 2 

Why does std::cin work in Bash shell, but not in Emacs shell?

https://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Subprocess-buffering.html says:

having an explicit flag to control their buffering behavior,
typically -i for interactive, or by a special environment variable. 

Would that fix it? Where does the -i flag go?

I am using Emacs 25.2.1 with Evil Spacemacs and Ivy on Linux.


The interactive program works when shell-commend is called with asynchronously option:

SPC SPC (counsel-M-x) async-shell-command

Enter a number: 2

From Emacs help describe-function shell-command:

shell-command is an interactive compiled Lisp function in ‘simple.el’.


If COMMAND ends in ‘&’, execute it asynchronously.
The output appears in the buffer ‘*Async Shell Command*’.
That buffer is in shell mode.  You can also use
‘async-shell-command’ that automatically adds ‘&’.

Otherwise, COMMAND is executed synchronously.  The output appears in
the buffer ‘*Shell Command Output*’.

Why would interactive program not work on a synchronous system? https://en.wikipedia.org/wiki/Asynchronous_system

I am thinking of binding async-shell-command to SPC !. Is there a disadvantage to always running async-shell-command instead of the default shell-command?

  • shell-command isn't intended for use with interactive programs like yours. You should try using shell, termor ansi-term for these. – user12563 Jun 25 '17 at 13:56

async-shell-command works with interactive programs. async-shell does not.

Emacs has OS command-line interfaces and terminal emulators. It's not always obvious which one to use. Here is a comparison: http://ergoemacs.org/emacs/emacs_shell_vs_term_vs_ansi-term_vs_eshell.html

When to use a dedicated terminal: http://ergoemacs.org/emacs/emacs_shell_vs_terminal.html

| improve this answer | |
  • eshell is an Emacs alternative to Unix shells, but the other things are just interfaces to those same shells. – npostavs Jun 26 '17 at 14:37

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.