# System commands

**URL:** <https://discourse.julialang.org/t/system-commands/10618>\
**Category:** General Usage\
**Created:** [April 30, 2018, 10:13am UTC](https://discourse.julialang.org/t/system-commands/10618 "2018-04-30T10:13:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 30, 2018, 10:13am UTC](https://discourse.julialang.org/t/system-commands/10618/1 "2018-04-30T10:13:06Z")

</div>

Hi,

I am want to run a system command (executing a file) from inside a code written in Julia. However, writing

```julia
run(`echo Hello`)

```

throws an error

```julia
could not spawn `echo Hello`: no such file or directory (ENOENT)
in run at base\process.jl:650
in #spawn#374 at base\process.jl:511
in setup_stdio at base\process.jl:499
in #375 at base\process.jl:512
in _jl_spawn at base\process.jl:360

```

What am I doing wrong?

Philippe

---

<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:** [April 30, 2018, 10:35am UTC](https://discourse.julialang.org/t/system-commands/10618/2 "2018-04-30T10:35:04Z")

</div>

Are you running Julia on Windows?

In Julia a `Cmd` is not run through a shell, but is instead exec’d directly.  
To run a `cmd` internal command, such as `echo`, try:

```julia
run(`cmd /c echo Hello`)

```

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [April 30, 2018, 11:52am UTC](https://discourse.julialang.org/t/system-commands/10618/3 "2018-04-30T11:52:48Z")

</div>

Yes, I am running on Windows (and I obviously should have specified that!).

OK, thank you - this helped 😀

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [May 11, 2018, 10:39am UTC](https://discourse.julialang.org/t/system-commands/10618/4 "2018-05-11T10:39:00Z")

</div>

More questions on the same subject. I am in Windows  
I wish to execute a batch file, say

```julia
C:\path\web.bat

```

If I double click on the file in Windows-Explorer, the file executes correctly. If I execute

```julia
run(`cmd /c C:\\path\\web.bat`)

```

it fails. I believe this is because it is executed from Julia’s home directory, and the batch file does not find some dependencies.

So my question, how do I get Julia to run  
a) cd C:\path  
b) web.bat

If I just write

```julia
run(`cmd /c cd C:\\path\\webapp`)
run(`cmd /c web.bat`)

```

things fail: `'web.bat' is not recognized as an internal or external command, operable program or batch file.` I believe because the “current directory” is lost from on run command ot the next. **How can I get `run` to execute a sequence of commands?**

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [May 11, 2018, 12:36pm UTC](https://discourse.julialang.org/t/system-commands/10618/5 "2018-05-11T12:36:58Z")

</div>

If you want to run a bat file, dont use `cmd /c`, only specify the path to the file.

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [May 11, 2018, 1:17pm UTC](https://discourse.julialang.org/t/system-commands/10618/6 "2018-05-11T13:17:25Z")

</div>

> [@Philippe\_Maincon1](#):
>
> If I just write
> 
> run(`cmd /c cd C:\\path\\webapp`)  
> run(`cmd /c web.bat`)
> 
> things fail: ‘web.bat’ is not recognized as an internal or external command, operable program or batch file. I believe because the “current directory” is lost from on run command ot the next. How can I get run to execute a sequence of commands?

(Not sure about Windows-specific concerns to do with `cmd`, but at least for the path handling part…) The current working directory is inheritied from Julia each time `cmd` is invoked. Therefore, you need to move Julia’s working directory and then run the command, not change directory inside a command which is going to end. One way to do this without actually changing Julia’s working directory forever is to do it temporarily like

```julia
cd("C:\\path\\webapp") do
  run(`cmd /c web.bat`)
end

```

---

<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:** [May 12, 2018, 7:06am UTC](https://discourse.julialang.org/t/system-commands/10618/7 "2018-05-12T07:06:40Z")

</div>

You could try:

```julia
run(`cmd /c cd c:\\path '&&' web.bat`)

```

Note that special characters should be quoted within Julia commands, so `&&` needs to be quoted.

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [May 14, 2018, 7:44am UTC](https://discourse.julialang.org/t/system-commands/10618/8 "2018-05-14T07:44:21Z")

</div>

Greg, Jmert, Chakravala

thank you, all. I have tested your suggestions and they work! 😁

Philippe
