Erlang Shell by Marcos Benevides
:ID: bcd04f60-0a4c-4213-90f7-139c0ddf5bef
Some features of the Erlang shell.
Basics
Compiling and Running “Hello World” in the Shell
-module(hello).
-export([start/0]).
start() ->
io:format("Hello world~n").
Running the hello.erl
program in the erlang shell.
$ erl
Erlang/OTP 26 [erts-14.2.5] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [jit:ns]
Eshell V14.2.5 (press Ctrl+G to abort, type help(). for help)
1> c(hello).
{ok,hello}
2> hello:start().
Hello world
ok
Compiling Outside the Erlang Shell
$ erlc hello.erl
$ erl -noshell -s hello start -s init stop
erlc
evokes the Erlang compiler from the command line. The compiler compiles the code inhello.erl
and produces an object code file calledhello.beam
.The
erl -noshell
command loads the modulehello
and evaluates the functionhello:start()
. After this, it evaluates the expressioninit:stop()
, which terminates the Erlang session. (Armstrong 2013, 14)
Commands
Seeing Your Bound Variables
b()
shows all bound variables in the current erlang shell.
> b().
N = 1
Number = 5
Clearing Bound Variables in the Shell
> f(N).
> f().
Getting Help
The Erlang shell has a number of built-in commands. You can see them all with the shell command
help()
. (Armstrong 2013, 173).
Compiling and running Erlang programs
Modifying the Environment
You can find the value of the current load path by starting an Erlang shell
and giving the command code:get_path()
. The two most common functions that we
use to manipulate the load path are as follows:
% Add a new directory to the start of the load path.
-spec code:add_patha(Dir) => true | {error, bad_directory}
% Add a new directory to the end of the load path.
-spec code:add_pathz(Dir) => true | {error, bad_directory}
Alternatively, you can start Erlang with a command like this:
$ erl -pa Dir1 -pa Dir2 ... -pz DirK1 -pz DirK2
Scripting
Often we want to be able to execute an arbitrary Erlang function from the OS command
line. The -eval
argument is very handy for quick scripting.
erl \
-eval 'io:format("Memory: ~p~n", [erlang:memory(total)]).' \
-noshell -s init stop
Run As an Escript
#!/usr/bin/env escript
main([A]) ->
I = list_to_integer(A),
F = fac(I),
io:format("factorial ~w = ~w~n", [I, F]).
fac(0) ->
1;
fac(N) ->
N * fac(N - 1).
Debugging
Reading Crash Dumps
If Erlang crashes, it leaves behind a file called erl_crash.dump
. To start the
analyzer, give the following command:
crashdump_viewer:start()