# Using elixir variable as System.cmd input

**URL:** <https://forum.elixirforum.com/t/using-elixir-variable-as-system-cmd-input/37568>\
**Category:** Questions\
**Tags:** system\
**Created:** [February 15, 2021, 9:46pm UTC](https://forum.elixirforum.com/t/using-elixir-variable-as-system-cmd-input/37568 "2021-02-15T21:46:14Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [February 16, 2021, 12:50am UTC](https://forum.elixirforum.com/t/using-elixir-variable-as-system-cmd-input/37568/3 "2021-02-16T00:50:38Z")

</div>

If you want pipes, you can use them.  
The documentation for System.cmd/2 mentions

> ## Shell commands
> 
> If you desire to execute a trusted command inside a shell, with pipes, redirecting and so on, please check [`:os.cmd/1`](http://www.erlang.org/doc/man/os.html#cmd-1).

so you can do

```elixir
iex(11)> :os.cmd('wc -l < foo.txt') |> :binary.list_to_bin()
"5\n"

```

If you want to read from a variable, you interpolate it, or set an environment variable

```elixir
iex(14)> System.put_env("foo", "bar baz")                     
:ok
iex(17)> :os.cmd('echo ${foo}') |> :binary.list_to_bin()        
"bar baz\n"
iex(18)> :os.cmd('echo ${foo} | wc') |> :binary.list_to_bin()   
" 1 2 8\n"

```

---

_[View the full topic](https://forum.elixirforum.com/t/using-elixir-variable-as-system-cmd-input/37568)._
