Noob question. I am not quite able to tell the difference between a REPL and an interactive shell just by reading the definitions on Wikipedia.
Wiki notes that REPL is a particular kind of interactive language shell. Is it a proper subset though? The Wiki definition seems to restrict the terminology REPL to Lisp-like languages, whereas the stated properties don’t really contain any distinguishing features.
In particular, would it be correct to call IPython a REPL?
REPL: This is a procedure that just loops, accepts one command at a time, executing it, and printing the result.
The three steps at each iteration of the loop are:
- Calling read to read the characters that make up a textual expression from the keyboard input buffer, and construct a data structure to represent it,
- Calling eval to evaluate the expression–intuitively, eval “figures out what the expression means,” and “does what it says to do,” returning the value of the expression–and
- Calling write to print a textual representation of the resulting from eval, so that the user can see it.
You can write your own read-eval-print loop for your own programs, so that users can type in expressions, and you can interpret them any way you want. You can start up your read-eval-print loop (by typing in (rep-loop)
), and it will take over from the normal Scheme read-eval-print loop, interpreting expressions your way.
Here’s a very simple read-eval-print loop:
(define (rep-loop)
(display "repl>") ; print a prompt
(write (eval (read))) ; read expr., pass to eval, write result
(rep-loop)) ; loop (tail-recursive call) to do it again
Notice that the expression (write (eval (read)))
does things in the proper read-eval-print order, because the argument to each procedure call is computed before the actual call.
Interactive Shell : An interactive shell reads commands from user input on a terminal. Among other things, such a shell reads startup files on activation, displays a prompt, and enables job control by default. The user can interact with the shell. That’s how the interactive shell name came into being. Let us consider this bash script :
#!/bin/bash
echo -n "Enter the value of variable 'var1': "
read var1
echo "var1 = $var1"
echo
echo -n "Enter the values of variables 'var2' and 'var3' "
echo =n "(separated by a space or tab): "
read var2 var3
echo "var2 = $var2 var3 = $var3"
# If you input only one value,
#+ the other variable(s) will remain unset (null).
exit 0
Now the script above interacts with the user, it asks the user to enter inputs based on which it does its calculations. That’s why it behaves like an interactive shell.
Similary, the python interpreter which most people use to learn python is an interactive one as it communicates with its user.
2
Technically, it’s correct to say that a shell is an instance of a REPL.
However, it isn’t a matter of program definition as it is one of common usage scenarios.
Bash, for example, is written in C, but it could well have been written in Python. At that point, if you talk about program features and abilities, would it be correct to say that Bash is a shell while Python isn’t?
You could also say that Shells are about running commands while REPLs are about running instructions and function calls. But can’t you run commands in REPLs (Python’s os.system, os.popen, etc), and can’t you run (or define) functions in Bash, using many of its built-ins?
As aforesaid, it’s a matter of usage. If you juggle files and ready-made programs, you’re using it as a shell. If you’re testing libraries or language semantics, it’s a REPL.
Hope that helps.
This StackOverflow question discusses that very question (among other things).
I am personally on the fence as regards interactive Python. But, if you want to call it a REPL, I don’t think anyone will be angry with you (there are slightly too many things that work in source code files, but not in the interactive shell for me to be entirely happy with calling it a REPL).
1
A command line interpreter also performs read, evaluate, print, loop functions, but I don’t think there is a difference beyond REPL being the word used in the context of interpreting high level languages whereas command line interpreter shell is used in the context of a command line interface or scripting language. Both of them have to maintain internal state such as variables and parameter state. Correct me if I’m wrong.