How to get Erlang's release version number from a shell?

Many programs return their version number with a command like:

$ program --version
program (platform info) v1.2.3

This is useful for scripting the installation or maintenance of the program, and some other controlled automation magic from System Admins and friends.

Problem

How to easily get the version number for Erlang (OTP)?

On the net

Here are some unsatisfactory solutions (from the trapexit forum and other tutorials/Erlang documentation):

Emulator

$ erl
1> erlang:system_info(otp_release).
"R13B03"

Hard to script. I have not found a way to have erl execute a single command from a shell prompt.

Release file

$ cat /usr/lib/erlang/releases/RELEASES
[{release,"OTP  APN 181 01","R13B03","5.7.4",
      [{kernel,"2.13.4","/usr/lib/erlang/lib/kernel-2.13.4"},
       {stdlib,"1.16.4","/usr/lib/erlang/lib/stdlib-1.16.4"},
       {sasl,"2.1.8","/usr/lib/erlang/lib/sasl-2.1.8"}],
      permanent}].

Parsing paradise (with shell).

An alternative could also be checking the install path, but that is not portable (my install path does not include the version, for one).

Personal context: I am writing a script to install the same version of RabbitMQ with plugins on several machines. Some plugins have minimal requirements on the OTP version, and it is how this question started.


 erl -eval 'erlang:display(erlang:system_info(otp_release)), halt().'  -noshell

The other answers only display major version as of OTP 17 (from docs for erlang:system_info). This works to display major and minor version on my development machine:

erl -eval '{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), io:fwrite(Version), halt().' -noshell

This reads from the appropriate file, as described in the docs.


(I'm adding this answer here since I've searched for this at least 3 times in the past three months)

Starting from version 17.0 releases have a new format in their version number (17.0, 17.1, ...) but erlang:system_info(otp_release). only returns the major version number.

In order to get the full version number it is necessary to check the contents of the OTP_RELEASE file under the already mentioned releases folder.

$ which erl
/usr/bin/erl
$ cd /usr/bin
$ ls -l erl
../lib/erlang/bin/erl
$ cd ../lib/erlang/
$ cat releases/17/OTP_RELEASE
17.3

EDIT

# Some versions seem to have OTP_VERSION instead of OTP_RELEASE
$ cat releases/17/OTP_VERSION
17.4

init docs, linked by 'man erl'.

-eval Expr

Scans, parses and evaluates an arbitrary expression Expr during system initialization. If any of these steps fail (syntax error, parse error or exception during evaluation), Erlang stops with an error message. Here is an example that seeds the random number generator:

% erl -eval '{X,Y,Z} = now(), random:seed(X,Y,Z).'

This example uses Erlang as a hexadecimal calculator:

% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])'  -s erlang halt
BF

If multiple -eval expressions are specified, they are evaluated sequentially in the order specified. -eval expressions are evaluated sequentially with -s and -run function calls (this also in the order specified). As with -s and -run, an evaluation that does not terminate, blocks the system initialization process.

Thus,

$ erl -noshell -eval 'io:fwrite("~s\n", [erlang:system_info(otp_release)]).' -s erlang halt