Command from a string

Hi there, how can I create & run a command from a string, e.g.

julia> str = "echo hello world"
"echo hello world"

julia> read(`$(str)`)
ERROR: IOError: could not spawn `'echo hello world'`: no such file or directory (ENOENT)

(assume str is a complex command and I’d like to perform my own escaping of arguments)

Thanks

Sounds like you are thinking of a shell command? To do that you want to spawn a shell:

julia> run(`sh -c $str`);
hello world

This is the analogue of the os.system function in Python or the system function in C.

Of course, the whole reason why Julia’s run commands are set up the way they are is to avoid spawning a shell and to avoid brittle manual escaping. See also this discussion: Better support for running external commands - #8 by stevengj

Could you give more context about why you are doing this?

2 Likes