Your first script

A script is a text file that Elixir reads from top to bottom and runs. The extension is .exs, the command is elixir, and the whole toolchain is the one the install lesson put on the machine. The file below is one line long, and it is a complete program.

The file hello.exs

Create an empty folder and put one file in it. The name is up to you; the course uses hello.exs. The only line calls IO.puts/1, which writes its argument to standard output and adds a newline:

hello.exs

IO.puts("Hello, Elixir")

IO is the module that talks to the terminal, puts is the function, and /1 is how the documentation writes its arity: one argument. There is no main function and no import line, because a script is executed as written.

Run it with elixir

Pass the file name to the command from the folder that holds it. The toolchain compiles the file in memory, runs it, and the greeting appears:

elixir hello.exs

Hello, Elixir

A script prints only what it asks to print. Remove the IO.puts and the command succeeds with no output at all, because the value of the last expression is not echoed: echoing is what the shell of the previous lesson does. This is the difference between a script and a session, and it surprises every reader once.

Interpolation and arithmetic

A double-quoted string can hold an expression between #{ and }. The expression is evaluated first and its text is placed in the string, so a value never needs a conversion call. The next script uses it twice, once with a binding and once with arithmetic:

greet.exs

name = "Ada" age = 36 IO.puts("Hello, #{name}") IO.puts("Next year you will be #{age + 1}")

elixir greet.exs

Hello, Ada
Next year you will be 37

The two bindings are ordinary variables, name holds a binary and age an integer, and the arithmetic happens inside the string before anything is printed. The next chapter takes variables and numbers apart; for now the shape of a script is enough: bindings at the top, calls below, and one line per step.

Two mistakes of a first run

The first mistake is a call without its module. Elixir has no bare puts, so the compiler stops and says which name it could not find:

puts.exs

puts("Hello, Elixir")

elixir puts.exs

    error: undefined function puts/1 (there is no such import)
    │
  1 │ puts("Hello, Elixir")
    │ ^
    │
    └─ puts.exs:1:1

** (CompileError) puts.exs: cannot compile file (errors have been logged)

The box shows the source line with a caret under the name, and the message itself is the first line. The fix is IO.puts("Hello, Elixir"), and the rule behind it is the one the shell lesson showed: a function belongs to a module.

The second mistake is the command without the extension. The command takes a file name, not a program name, so elixir hello looks for a file called hello and finds nothing:

elixir hello

No file named hello

The toolchain does not guess the extension. Write the whole name, hello.exs, or press Tab after the first letters and let the shell complete it.

The scripts on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

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

  1. What does elixir hello.exs do?

    Answer

    It runs the script in the file hello.exs

    The command takes a script file and executes it from the first line to the last.

  2. What does a script print when it has no IO call at all?

    Answer

    Nothing, the values are not echoed

    Echoing values is a feature of the shell; a script prints only through IO.

  3. What extension does an Elixir script use?

    Answer

    .exs

    A script is .exs and a compiled source file is .ex; the bytecode is .beam.

  4. What does this program print? IO.puts("2 + 2 is #{2 + 2}")

    Answer

    2 + 2 is 4

    The expression inside #{} is evaluated before the string is printed.

  5. What does this program print? name = "Ada" IO.puts("Hello, #{name}")

    Answer

    Hello, Ada

    Interpolation places the value of the binding in the string, without quotes.

  6. What does this program print? IO.puts("one") IO.puts("two")

    Answer

    one / two

    Each IO.puts/1 writes its own line, in the order the lines run.

  7. What does this program print? age = 36 IO.puts(age + 1)

    Answer

    37

    The binding holds the integer 36, and 36 + 1 is 37.

  8. What does this program print? IO.puts("Hello" <> ", " <> "world")

    Answer

    Hello, world

    <> joins two binaries at a time, and the result is joined to the third one.

  9. What does elixir hello print when the file is hello.exs?

    Answer

    No file named hello

    The command reads a file name and does not add the .exs extension.

  10. What does puts("hi") in a script give?

    Answer

    A CompileError: undefined function puts/1, because IO was not named

    The function is IO.puts/1; a bare puts/1 does not exist.

Article author: Arthur Isaev