I’m trying to run a shell command with a pipe operator in it. For example “sleep 0.1 | echo foo”. At first I tried to do this with System.cmd:
System.cmd("sleep", ["0.1", "|", "echo", "foo"])
But this results in /usr/bin/sleep: invalid time interval '|', invalid time interval 'echo'....
So next I tried to use :os.cmd:
:os.cmd("sleep 0.1 | echo foo")
But whatever I do to execute this I get:
:os.cmd("sleep 0.1 | echo foo")
** (FunctionClauseError) no function clause matching in :os.validate/1
The following arguments were given to :os.validate/1:
# 1
"sleep 0.1 | echo foo"
(kernel) os.erl:281: :os.validate/1
(kernel) os.erl:237: :os.cmd/1
System.cmd does not run a shell, so pipes, redirects, and so on aren’t supported. System.cmd is basically the closest thing that Elixir has to fork.
:os.cmd is definitely handy, but take great care if any of the stuff in your command comes from users (like filenames). You risk letting users run arbitrary programs.
It is strange that :os.cmd accepts an atom for the command name, looks like a historical accident.
In general, functions from the Erlang standard library take character lists where in Elixir you would use a string. To make it more confusing, the Erlang type for such lists is called string(). I recommend taking a look at this cheat sheet if you still feel confused.
Going back to your problem, Ben has already done a good job explaining the difference between Elixir’s System.cmd and Erlang’s :os.cmd. In the source code for :os.cmd you can see it actually spawns a shell appropriate for the current OS and evaluates the string you’ve passed to the function in that shell.
Replicating this behaviour for your particular example using System.cmd is easy:
Not entirely. A lot of the ‘base’ functions get the string value of atoms, it makes it cheap and efficient to pass around something like sed instead of having to do "sed" in the original erlang (plus it’s fantastic since atoms in erlang can have @ in them without quoting too!). Calling other programs was generally used to access their stdin/stdout, not for argument parsing and ‘doing’ other stuff.
(Do note, the sed and "sed" above in erlang, in elixir would be :sed and 'sed' respectively.)
Overall less space for repeatedly used ‘strings’, atoms are essentially interned/flyweight’d strings after all (and lists of integers are anything but lightweight in memory, even if they are very fast iterables). Not really an issue nowadays, but Erlang is old remember. ^.^
As for the @ bit, it’s because they are used in areas on the VM such as server names, cookies, etc… Like in elixir you’d have to do :"username@server" where in erlang you’d just do username@server, nothing else needed, which is a great boon when manually doing a lot of work at the console or putting those in erlang config files. But of course, the ecosystem handles that better now and so things like Elixir don’t even support @ in unquoted atoms unlike Erlang. ^.^;