# Run echo using Sys.command

**URL:** <https://discuss.ocaml.org/t/run-echo-using-sys-command/10085>\
**Category:** Learning\
**Created:** [June 28, 2022, 3:45pm UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085 "2022-06-28T15:45:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke36](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@luke36](https://discuss.ocaml.org/u/luke36)\
**Post date:** [June 28, 2022, 3:45pm UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085/1 "2022-06-28T15:45:29Z")

</div>

I am trying to print colored texts in terminal using echo -e ‘\x1b[31mhello world\x1b[0m’. Everything is well in the terminal when I type this myself, but the code below outputs “-e \x1b[31mhello world\x1b[0m” itself. What’s the problem? How can I fix it?

```auto
let _ = 
  ignore (Sys.command ("echo -e '\\x1b[31mhello world\\x1b[0m'"))

```

I am working in ubuntu, WSL2 to be exact.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [June 28, 2022, 4:13pm UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085/2 "2022-06-28T16:13:13Z")

</div>

Properly using `Sys.command` needs quoting technology [you likely don’t want to know about](https://github.com/ocaml/ocaml/pull/1492).

Is there any strong reason for you not to use [`Unix.create_process`](https://v2.ocaml.org/api/Unix.html#VALcreate_process) or simply [`print_endline`](https://v2.ocaml.org/api/Stdlib.html#VALprint_endline) ? In these cases WYSYWG.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [June 28, 2022, 4:46pm UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085/3 "2022-06-28T16:46:29Z")

</div>

> [@luke36](#):
>
> How can I fix it?

You need to stop quoting the backslash:

```auto
let _ = 
  ignore (Sys.command ("echo -e '\x1b[31mhello world\x1b[0m'"))

```

> [@dbuenzli](#):
>
> Is there any strong reason for you not to use [`Unix.create_process`](https://v2.ocaml.org/api/Unix.html#VALcreate_process) or simply [`print_endline`](https://v2.ocaml.org/api/Stdlib.html#VALprint_endline) ? In these cases WYSYWG.

Ditto.

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [June 28, 2022, 8:58pm UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085/4 "2022-06-28T20:58:18Z")

</div>

Heh indeed. It is for this very reason that the helper scripts I wrote for `camlp5` and `pa_ppx` that need to do all sorts of shell-quoting … are written in Perl, and not in OCaml. I simply didn’t want to figure out all the quoting b.s., and it wasn’t relevant to the goals of the actual code being implemented.

If I had to do this, I’d sooner learn how to use terminfo (or termcap, or ncurses) from OCaml, than try to use the shell.

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ninjaaron/32/1885_2.png) [@ninjaaron](https://discuss.ocaml.org/u/ninjaaron)\
**Post date:** [June 29, 2022, 6:40am UTC](https://discuss.ocaml.org/t/run-echo-using-sys-command/10085/5 "2022-06-29T06:40:09Z")

</div>

`Filename.quote_command` … Lest we reinvent the wheel.
