Run echo using Sys.command

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?

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

I am working in ubuntu, WSL2 to be exact.

Properly using Sys.command needs quoting technology you likely don’t want to know about.

Is there any strong reason for you not to use Unix.create_process or simply print_endline ? In these cases WYSYWG.

1 Like

You need to stop quoting the backslash:

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

Ditto.

Cheers,
Nicolas

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.

Filename.quote_command … Lest we reinvent the wheel.