81

In Emacs I can run a shell using following commands -

M-x term

M-x shell

M-x eshell

What is the difference between these three?

cjm
  • 27,160

2 Answers2

84

shell is the oldest of these 3 choices. It uses Emacs's comint-mode to run a subshell (e.g. bash). In this mode, you're using Emacs to edit a command line. The subprocess doesn't see any input until you press Enter. Emacs is acting like a dumb terminal. It does support color codes, but not things like moving the cursor around, so you can't run curses-based applications.

term is a terminal emulator written in Emacs Lisp. In this mode, the keys you press are sent directly to the subprocess; you're using whatever line editing capabilities the shell presents, not Emacs's. It also allows you to run programs that use advanced terminal capabilities like cursor movement (e.g. you could run nano or less inside Emacs).

eshell is a shell implemented directly in Emacs Lisp. You're not running bash or any other shell as a subprocess. As a result, the syntax is not quite the same as bash or sh. It allows things like redirecting the output of a process directly to an Emacs buffer (try echo hello >#<buffer results>).

cjm
  • 27,160
  • 1
    eshell can not distinguish between stdout and stderr. If you redirect in eshell, you always redirect both. – ceving Feb 21 '22 at 09:46
24

According to Xahlee page:

shell is the standard emacs interface to Operating System's command line interface.

term (ansi-term is pretty much same to term today. They were different packages, but now both defined in term.el) is a terminal emulator. It behaves like a dedicated terminal app, such as {xterm, gnome-terminal, puTTY}. It is compatible to more shell apps than emacs shell interface, but standard emacs keys such as moving cursor don't work here (because it is emulating a terminal.)

eshell is a shell written entirely in emacs lisp. Note: it is not a bash emulator. Eshell is a shell by itself, but similar to bash or other shells.

Which should you use?

It depends on your preference.

shell is good for general use of classic/standard unix shell commands, such as {grep, du, ls, sort, cat, head, tail, uname, …}.

term & ansi-term are good if you want to run stuff like ssh, or other command line interactive interface (such as {python, ruby, lisp} shell), or text based GUI app such as {vim, synaptic, …}.

eshell is good especially on Microsoft Windows where bash is not installed, or, if you are a emacs lisp programer, because eshell has direct access to emacs lisp.

CodyChan
  • 954
  • All the commands you mentioned (grep, du, ls, etc.) are programs and commands built in the shell. eshell will have them too if you have those programs installed. – Bart Louwers Jan 08 '17 at 23:27
  • 2
    @Bart try to remove your grep in your system and do grep action in eshell, you'll find that the grep in eshell is not grep in your system, which grep in eshll says "eshell/grep is a "compiled Lisp function in `em-unix.el', of cause the grep in your system can be executed in eshell, like I said eshell is very useful in MS which a lot small tools like grep are not installed. – CodyChan Jan 09 '17 at 05:40
  • "term ... standard emacs keys such as moving cursor don't work here". Not exactly, in term, we can switch between char and line modes. In the line mode, we can use standard emacs keys. https://www.gnu.org/software/emacs/manual/html_node/emacs/Terminal-emulator.html – SergiyKolesnikov Oct 01 '22 at 19:24