How to use the command "iex" (with examples)
Code
iex
Motivation
The command iex
is used to start an interactive session in the Elixir language. This allows developers to execute Elixir code and interact with the system in real-time.
Explanation
Running iex
without any arguments will start an interactive session where you can execute Elixir code and see the results immediately. This is useful for testing code snippets, exploring libraries, or experimenting with language features.
Example Output
Interactive Elixir (1.11.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Using command “iex –erl “-kernel shell_history enabled””
Code
iex --erl "-kernel shell_history enabled"
Motivation
When working with iex
, it can be helpful to have a history of previously executed commands. This allows you to easily recall and reuse commands that were previously entered.
Explanation
By using the --erl "-kernel shell_history enabled"
argument, the iex
command enables the shell history feature. This feature records all the commands entered in the interactive session, allowing you to navigate and reuse them using keyboard shortcuts.
Example Output
Interactive Elixir (1.11.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Using command “iex -S mix”
Code
iex -S mix
Motivation
In Elixir, Mix is a build tool that helps in creating, compiling, and managing projects. By starting an iex
session with the -S mix
argument, you can load the Mix project files and have access to the project’s modules, dependencies, and configuration.
Explanation
The -S mix
argument is used with the iex
command to start an interactive session while loading the Mix project files. This allows you to interact with the project-specific code and dependencies.
Example Output
Compiling 1 file (.ex)
Generated my_app app
Interactive Elixir (1.11.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Conclusion
In this article, we explored different use cases of the iex
command in Elixir. We demonstrated how to start an interactive session, enable the shell history feature, and load Mix project files. These examples showcase the versatility of the iex
command and how it can be used for different purposes in Elixir development.