LinuxCommandLibrary

iex

Start an Elixir interactive shell

TLDR

Start an interactive session

$ iex
copy

Start a session that remembers history
$ iex --erl "-kernel shell_history enabled"
copy

Start and load Mix project files
$ iex -S mix
copy

SYNOPSIS

iex [options] [file]
iex [options] --sname <node_name>
iex [options] --name <node_name>
iex [options] --remsh <node_name>

PARAMETERS

[file]
    Specifies an Elixir script file to load and execute before starting the shell. This is typically used for running one-off scripts or preloading modules.

--help
    Displays the comprehensive help message for iex, listing all available options and their usage.

--version
    Prints the version information for iex and the underlying Elixir distribution.

--sname <name>
    Sets the short node name for the Erlang VM, enabling distributed Elixir features like connecting to other nodes. This name is local to the machine.

--name <name>
    Sets the long node name for the Erlang VM, enabling distributed Elixir features across networks. This name typically includes the host.

--cookie <cookie>
    Sets the Erlang distributed cookie, which must match among nodes for them to communicate securely in a distributed system.

--remsh <node>
    Connects to a remote Elixir node identified by <node> (e.g., 'foo@localhost') and starts an interactive shell on that node.

--dot-iex <file>
    Specifies an alternative file to load instead of the default ~/.iex.exs startup script when iex launches.

--no-halt
    Prevents the iex shell from automatically halting and exiting after a specified script or evaluation finishes execution.

--eval <code_string>
    Evaluates the given Elixir code string before starting the interactive shell. Useful for one-liner tasks or setup.

--require <file>
    Requires and compiles the specified Elixir file or module before starting the shell, making its functions available.

--import <file>
    Imports the specified Elixir file and makes its functions available directly in the current scope of the iex session.

--pa <path>
    Prepends the specified directory to the code path, allowing the Erlang VM to find and load modules from that location first.

--pz <path>
    Appends the specified directory to the code path, allowing the Erlang VM to find and load modules from that location.

DESCRIPTION

iex is the Interactive Elixir shell, a powerful Read-Eval-Print Loop (REPL) environment for the Elixir programming language. It allows developers to interactively execute Elixir code, test snippets, experiment with language features, and debug applications in real-time. Built on the Erlang VM (BEAM), iex provides access to Elixir's modules, functions, and processes, making it an indispensable tool for development, exploration, and introspection of Elixir projects. It can be used to run individual commands, start a Mix project's shell, or connect to a running Elixir node for distributed debugging.

CAVEATS

iex is part of the Elixir programming language ecosystem and requires both Elixir and Erlang/OTP to be installed on the system. It is not a standalone operating system utility. While excellent for development and debugging, it's generally not intended for running long-lived production applications, which are typically compiled and deployed as releases. The state within an iex session is ephemeral and is lost once the session is terminated.

INTEGRATION WITH MIX

When run within an Elixir project directory, iex is commonly invoked via mix iex. This command not only launches the iex shell but also loads the project's dependencies, compiles its source code, and sets up the application environment, making the project's modules and applications readily available within the interactive session.

REMOTE SHELL CAPABILITIES

One of iex's powerful features is its ability to connect to and interact with remote Elixir nodes running on the same or different machines. Using options like --sname, --name, and --remsh, developers can debug, inspect processes, and even manage running applications in a distributed environment, providing unparalleled insight into production systems.

CUSTOMIZATION

Upon startup, iex automatically loads and executes the ~/.iex.exs script (or a specified file with --dot-iex). This file can be used to define custom helper functions, set aliases, configure the environment, or perform any setup tasks, allowing developers to tailor their interactive Elixir experience and streamline their workflow.

HISTORY

iex has been a fundamental component of the Elixir programming language since its inception by José Valim in 2012. Designed to foster an interactive and exploratory development workflow, its presence highlights Elixir's commitment to developer experience. The functionality of iex, including its ability to connect to remote nodes and load project contexts, has evolved alongside the language, drawing inspiration from and improving upon interactive shells in other languages like Ruby's IRB and Python's IDLE.

SEE ALSO

elixir(1), mix(1), erl(1)

Copied to clipboard