I would like to understand what is the difference between
M-x shell and
M-x term? Also under which circumstances should be used the first and under which the second?
shell is the oldest of the two. 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
less inside Emacs).