erl: the interactive shell
Start erl
Run erl with no arguments to start a local shell. The prompt counts up from one, and every answer comes back as a value you can use in the next line.
erl
Erlang/OTP 27 [erts-15.2.7.14] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
Eshell V15.2.7.14 (press Ctrl+G to abort, type help(). for help)
1>
Type an expression and end it with a period. The shell prints the value, then waits for the next one.
Evaluate expressions
Any expression works at the prompt: arithmetic, an atom, or a list.
Strings print as lists of character codes unless you ask for a readable form, which lesson 40 covers.
erl
1> 1 + 2.
3
2> hello.
hello
3> [hello, world].
[hello,world]
Shell helpers
The help function lists every internal command. The h function shows the history, b lists the current bindings, and q halts the node cleanly.
erl
1> b().
ok
2> Name = "Ada".
"Ada"
3> b().
Name = "Ada"
ok
4> h().
1: b()
-> ok
2: Name = "Ada"
-> "Ada"
3: b()
-> ok
ok
5> q().
ok
Recall results
The v function recalls a result by its query number, and v(-1) recalls the last one. A recalled value works in any later expression.
Because v returns an ordinary value, it can sit inside a longer expression, as the last line does.
erl
1> 2 + 2.
4
2> v(1).
4
3> v(-1) * 10.
40
Next: Your first Erlang script
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does an expression end in the Erlang shell?
Answer
With a period
The period, not the newline, tells the shell that the expression is complete.
-
What does the shell answer to
1 + 2.?Answer
3The shell prints the value of the expression, and 1 + 2 is 3.
-
What does the shell print for
[hello, world].?Answer
[hello,world]The shell prints values without spaces after the commas, so the list appears as
[hello,world]. -
What is
helloin the expressionhello.?Answer
An atom
A lower-case name without quotes is an atom, and it evaluates to itself.
-
Which function lists the internal commands of the shell?
Answer
help()help()prints the command list; the other three answer history, bindings and stored values. -
What does
h().show?Answer
The history of queries and their results
h()numbers the earlier queries and prints the value each one returned. -
What does
q().do?Answer
Stops the node cleanly
q()is shorthand forinit:stop(), which shuts the node down without an error. -
Which function returns the value of query N?
Answer
v(N)v(N)gives the value of query N, ande(N)would run that query again instead. -
What does
v(-1).return?Answer
The value of the last query
A negative number counts back from the newest result, so
-1is the one just printed. -
What does
b().display?Answer
All variable bindings
b()lists the names bound in the shell together with their values.
The examples on this page were run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev