How to pass strings containing commas to a script using `mix run`?

Apologies if I am missing something silly, but I am unable to figure this out. I am trying to run an Elixir script using mix run, and I want to pass a single arbitrary string to it.

A sample script, say scripts/string_parser.exs looks like this:

System.argv()
|> IO.inspect()

This example only contains the relevant parts. Here’s some sample runs of the script:

> mix run scripts/string_parser.exs '{:ok}'
["{:ok}"]

> mix run scripts/string_parser.exs '{:ok,:test}' 
["{:ok", ":test}"]

> mix run scripts/string_parser.exs '{:ok\,:test}'
["{:ok\" :test}"]

> mix run scripts/string_parser.exs "{:ok,:test}" 
["{:ok", ":test}"]

> mix run scripts/string_parser.exs "{:ok\,:test}"
["{:ok\" :test}"]

How do I pass a string that contains a , as a command line argument to mix run <script>?

Note: This issue does not occur for running a script with elixir <script> <arguments>. For example,

> elixir scripts/string_parser.exs '{:ok, :test}'  
["{:ok, :test}"]

and all the other variations work as expected.

This either seems like a bug or I’m missing something.

1 Like

use "

Edit: just saw, that you already tested that.
And that should work.

$ mix run test.exs "{:ok,:test}" 
["{:ok,:test}"]

at least it does for me (linux, bash)

It also works as expected for me. (macOS, zsh)

Perhaps you might want to share which OTP and Elixir version you are on, and probably also which operating system and shell you are using.

$ elixir -v
Erlang/OTP 25 [erts-13.1] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]

Elixir 1.14.0 (compiled with Erlang/OTP 25)

Also these two (run # 3 and 5) runs look like perhaps your shell was doing something weird when escaping characters.

@bmitc are you on Windows by any chance? mix is mix.bat or mix.ps1 and I think you’ll find that passing args on Windows is a nightmare due to the different shells and batch scripts vs PowerShell scripts.

My determination was that it is not worth the effort trying to handle them and built an exe for my release instead of <release>.bat. The Burrito project does something funky to handle the args on Windows too.

You might have better luck doing your dev in WSL.

1 Like

I am indeed on Windows. I should have mentioned that, but it actually didn’t occur to me since elixir works as expected. I am on Windows 11 using PowerShell. My Elixir version is:

> elixir --version
Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit]

Elixir 1.13.3 (compiled with Erlang/OTP 22)

I typically bounce between Windows and WSL2 and sometimes Docker for my development, depending on what I’m doing.

Does anyone know why elixir works and mix doesn’t? It doesn’t seem to be solely a Windows problem if one works as expected, although I seem to recall running into something similar to this before with mix. Since Elixir supports Windows, I think this should probably work or be documented why. It would be great to understand why it doesn’t work. I haven’t read through the elixir and mix scripts in detail on Windows.