elixir

How can I make a single-file script with Elixir, without excess output?


I would like to do something that seems like it should be simple, namely make a script that outputs a greeting message and nothing else. This script is so simple that it shouldn't require more than one file. Here's what I tried:

[ ~/elixir ] cat hello.exs
#!/usr/bin/env iex
IO.puts "Hello world!"
:init.stop

[ ~/elixir ] ./hello.exs
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Hello world!
Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [ ~/elixir ]

This is pretty close to the desired result, but I'm looking for something like a --quiet or --silent flag to suppress the Erlang and Elixir startup messages. Adding the -S flag to iex doesn't help:

[ ~/elixir ] cat hello.exs
#!/usr/bin/env iex -S
IO.puts "Hello world!"
:init.stop

[ ~/elixir ] ./hello.exs
Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

-S : Could not find executable ./hello.exs

I know it's possible (https://abstraction.killedthecat.net/create-command-line-utility-elixir-mix/) to use mix to create a project for this, but it seems like overkill. All I want to do right now is make a simple hello script.


Solution

  • You should use the elixir executable to run Elixir files (#!/usr/bin/env elixir). iex will execute the file and start the Interactive Shell.