Julia spawn several commands


#1

Hi Julia users,

I would like to run a command that is actually several commands separated by semi-colons.

ex:

run(`ls; ls`)

I get an error that semi-colons needs to be quoted.

  • run('ls; ls') does not work
  • run(ls';' ls) does not work
  • run([ls, ls]) does not work

I intend to run the command in a Process using spawn, so doing run(ls); run(ls) does not work either.

Is there a good workaround?


#2

WARNING: special characters "#{}()[]<>|&*?~;" should now be quoted in commands

Try:

run(`ls\; ls`)

#3

actually, that doesn’t work either (it gives ERROR: could not spawn‘ls;’ ls: no such file or directory (ENOENT)). The reason is that ; is a shell feature, and Julia commands aren’t run through the shell at all. That’s also why globbing, $-expansion, and aliases don’t work in Julia commands.

@tawheeler what’s wrong with:

run(`ls`)
run(`ls`)

?

Or, if you really want to use shell features, you can invoke the shell:

run(`bash -c "ls; ls"`)

but then you lose Julia’s nice handling of quoting and escaping and are instead back in the wild west of shell syntax.


#4

I wanted to call them in the same spawn process. So actually, spawn(ls; ls). It isn’t the end of the world if this doesn’t work, but I figured there was just some simple thing I was missing.
Thanks!


#5

You can combine Cmd objects with &:

spawn(`ls` & `ls`)

which returns something called a ProcessChain… which sounds promising? Thought I think it might kick them off asynchronously.


#6

Thanks - I can just put what I need in a bash script and call that.


#7

Why? Even if they are originally in a single process like a shell that then has to run them as separate processes.


#8

I want to first prepare env variables and then call a ros command.


#9

It may be easier to use the withenv function in Julia.