The shell as a tool

The Erlang shell is not only a place to evaluate an expression. It lists files, changes directory, compiles and reloads modules, remembers the records of a beam, forgets a binding and runs a script with arguments. The previous lesson introduced the prompt and its helpers; this one is the rest of the toolbox.

Navigate the file system

pwd/0, cd/1, ls/0, ls/1, mkdir/1 and rm/1 are functions of the shell, so each one takes parentheses and a full stop like any other call. They change the working directory of the node, which is also the first folder of the code path, so a cd moves both the file commands and the loader.

erl

1> cd("/tmp"). /tmp ok 2> pwd(). /tmp ok

cd/1 prints the new directory and then returns ok, which is why the transcript has two answer lines for one query. Written without parentheses, ls is the atom ls and is not a call at all: the shell answers ls, not a file list. That is the first mistake of every shell function.

Compile and load a module

c(Module) compiles the module source and loads the result; l(Module) loads a beam that is already on the code path, and nl(Module) loads it on every connected node. The shell is the fastest way to try a module without a project. Save this as wordcount.erl:

wordcount.erl

-module(wordcount). -export([words/1, count/1]). words(Text) -> string:lexemes(Text, " "). count(Text) -> length(words(Text)).

Compile it once with erlc, then load it with l/1 and call the exported functions. The answer to l/1 is the module the code server just loaded:

erlc wordcount.erl

erl

1> l(wordcount). {module,wordcount} 2> wordcount:words("one two three"). ["one","two","three"] 3> wordcount:count("one two three"). 3

c/1 needs the source, l/1 only the beam, and m(Module) prints what the loaded module exports together with its compile information. The functions live in the node, not in the shell: a cd/1 after the load does not unload anything, and l/1 is the way to replace a module after a change.

Records at the prompt

A record is a tagged tuple with named fields, and the shell can hold a record definition without a module. rd(Name, {Field, ...}) defines one, rl() lists them, rp(Term) prints a term with their field names, and rf(Name) forgets one. A term that was printed as #person{...} is still a tuple; the definition only changes how the shell prints and builds it.

erl

1> rd(person, {name, age}). person 2> P = #person{name = "Ada", age = 36}. #person{name = "Ada",age = 36} 3> rp(P). #person{name = "Ada",age = 36} ok 4> P#person.age. 36 5> rf(person). ok

rr(Module) reads the record definitions out of a compiled beam, which is how a shell picks up the records of a library without copying them by hand. The Records lesson writes the same definition in a module, where it belongs in a real program.

Bindings, history and scripts

The shell keeps the variables you bind, and f/0 and f/1 forget them: f(X) frees one name and f() frees every binding. This is useful when a name is bound to a value that the next experiment must replace, because a second match with another value is an error. The transcript also clears the record example of the section above, so its binding does not follow the experiment.

erl

1> X = 1. 1 2> f(). ok 3> b(). ok

b() lists one line per binding above its ok; here every name is gone and the answer is the ok alone. A forgotten name is not bound at all, so the next line may bind it to something else, while a name that still holds a value fails the match. history(N) and results(N) set how many queries and answers the shell keeps, and the arrow keys walk through them.

A script runs from the same shell with its arguments after the file name. An escript receives them as a list of strings in main/1, and the length is one call away. Save this as count_args.escript:

count_args.escript

#!/usr/bin/env escript %%! -noshell -noinput main(Args) -> io:format("~p argument(s)~n", [length(Args)]).

chmod +x count_args.escript

escript count_args.escript one two three

3 argument(s)

Leave the shell

q() stops the node cleanly and prints ok; halt() stops it at once, without running the shutdown hooks. Ctrl+G opens the user switch menu: q quits, s starts a new shell, and c connects to another node. Ctrl+C interrupts the running query, and a second Ctrl+C ends the node. Ctrl+D at an empty prompt closes the shell and the node with it, as q() does.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is a shell function such as pwd called?

    Answer

    pwd().

    A shell function is an ordinary call: parentheses, the arguments, and the full stop that ends the query.

  2. What does l(wordcount). do?

    Answer

    Loads the beam of wordcount from the code path

    l/1 asks the code server to load a module whose beam is already on the code path; c/1 is the compiling one.

  3. What is the difference between c/1 and l/1?

    Answer

    c/1 compiles the source, l/1 loads a beam

    c/1 runs the compiler and loads the result; l/1 only loads what a compiler already wrote.

  4. Which function defines a record at the shell prompt?

    Answer

    rd/2

    rd(Name, Fields) defines the record; rr/1 reads one from a beam and rf/1 forgets it.

  5. What does f(X). do?

    Answer

    Forgets the binding of X

    f/1 removes one binding and f/0 removes them all, so the name can be bound again.

  6. What does rl(). print?

    Answer

    The record definitions the shell holds

    rl/0 lists the record definitions, rl/1 prints the fields of one of them.

  7. What does this escript print? #!/usr/bin/env escript %%! -noshell -noinput main(_) -> Args = ["one", "two"], io:format("~p~n", [length(Args)]).

    Answer

    2

    Args is a list of two strings, and length/1 of it is 2.

  8. What does this module print? -module(q1). -export([main/0]). words(Text) -> string:lexemes(Text, " "). main() -> io:format("~p~n", [length(words("a b c"))]).

    Answer

    3

    string:lexemes/2 returns the three words, and length/1 is 3.

  9. What does this program print? -module(q2). -export([main/0]). main() -> P = {person, "Ada", 36}, io:format("~p~n", [element(3, P)]).

    Answer

    36

    A tuple is indexed from 1, so element 3 is the age, 36.

  10. What does this program print? -module(q3). -export([main/0]). main() -> io:format("~p~n", [length("Erlang")]).

    Answer

    6

    A string is a list of character codes, and the list has six elements.

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