# Julia spawn several commands

**URL:** <https://discourse.julialang.org/t/julia-spawn-several-commands/7421>\
**Category:** General Usage\
**Created:** [November 30, 2017, 8:58pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421 "2017-11-30T20:58:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [November 30, 2017, 8:58pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/1 "2017-11-30T20:58:43Z")

</div>

Hi Julia users,

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

ex:

```julia
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?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 30, 2017, 11:53pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/2 "2017-11-30T23:53:26Z")

</div>

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

Try:

```julia
run(`ls\; ls`)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 1, 2017, 3:46am UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/3 "2017-12-01T03:46:54Z")

</div>

> [@greg\_plowman](#):
>
> run(`ls\; ls`)

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:

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

```

?

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [December 1, 2017, 5:33am UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/4 "2017-12-01T05:33:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [December 1, 2017, 11:05am UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/5 "2017-12-01T11:05:11Z")

</div>

You can combine Cmd objects with `&`:

```julia
spawn(`ls` & `ls`)

```

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

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [December 1, 2017, 4:09pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/6 "2017-12-01T16:09:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 1, 2017, 4:30pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/7 "2017-12-01T16:30:25Z")

</div>

> [@tawheeler](#):
>
> I wanted to call them in the same spawn process.

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

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [December 1, 2017, 4:54pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/8 "2017-12-01T16:54:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 1, 2017, 4:55pm UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/9 "2017-12-01T16:55:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lionisxn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionisxn/32/37989_2.png) [@lionisxn](https://discourse.julialang.org/u/lionisxn)\
**Post date:** [May 19, 2023, 9:19am UTC](https://discourse.julialang.org/t/julia-spawn-several-commands/7421/10 "2023-05-19T09:19:57Z")

</div>

I encounter a similar problem as yours, my solution is to create an iobuffer to emulate a script, and run the script. For instance, we want to run powershell commands to sleep a computer through Julia, we know the working powershell commands like

> Add-Type -Assembly System.Windows.Forms  
> [System.Windows.Forms.Application]::SetSuspendState(“Suspend”, $false, $true)

Then, the julia code can be written as

> ```
> io = IOBuffer()
> write(io, """Add-Type -Assembly System.Windows.Forms
> [System.Windows.Forms.Application]::SetSuspendState("Suspend", \$false, \$true)""")
> run(`powershell $(String(take!(io)))`)
> 
> ```

In this way, you could theoretically execute any complex command sequence without using pipeline.
