Fail to run shell command

Hei,

In Julia 1.4, in Atom-IDE’s REPL, Windows, Julia and I have the following dialog

julia> run(`echo hello`)
ERROR: IOError: could not spawn `echo hello`: no such file or directory (ENOENT)
Stacktrace:
 [1] _spawn_primitive(::String, ::Cmd, ::Array{Any,1}) at .\process.jl:99
 [2] #550 at .\process.jl:112 [inlined]
 [3] setup_stdios(::Base.var"#550#551"{Cmd}, ::Array{Any,1}) at .\process.jl:196
 [4] _spawn at .\process.jl:111 [inlined]
 [5] run(::Cmd; wait::Bool) at .\process.jl:439
 [6] run(::Cmd) at .\process.jl:438
 [7] top-level scope at none:0

The command I type is straight from Julia’s manual, so I am baffled. Any idea?

echo is not a program that is available in the Windows command path by default. (It’s a Unix thing.)

1 Like

In Linux echo is an application. In windows it’s a feature of the cmd console. Try

run(`cmd /k echo hello`)

I did open a cmd prompt, and echo hello worked wonders.
And your suggestion works. :grinning:
So this means my first attempt sends a command to another shell, while yours sends a command to the same shell I opened?

One of these days it would be nice to get reasonable shell functionality on Windows by default. We’ve been back and forth on many different possible solutions, but the simplest seems to be to ship with busybox and put that in the path: see this comment by @StefanKarpinski.

Not a lot of people want to work on it, though. Spawning command-line programs on Windows can be really awkward under the hood because there isn’t a proper way to pass command-line arguments as a list of strings, only a set of not-completely-universal parsing rules. See also https://github.com/JuliaLang/julia/pull/33465 which painfully implemented this.

2 Likes
run(`cmd /k dir´)

does not work. Though

run(`cmd /k´) 

switches to a shell, ctrl-c to exit. …but that does not help to have Julia code generate DOS commands.

Steven, I’ll look at the winsomely thing.

Thanks, I’ll go get myself Linux… :slightly_smiling_face:

That didn’t work?!?!? That should have worked. It works on my 1.5.3. :confused:

Not really: Julia doesn’t use a shell to run a program. It just runs the program directly.

With

run(`echo hello`)

you ask Julia to execute the program echo (Windows will look for echo.exe, echo.com, etc.) with argument hello. This program exists on Linux but not on Windows.

With

run(`cmd /k echo hello`)

you ask Julia to execute the program cmd with arguments /k, echo and hello. The program cmd.exe is actually a shell so that will execute a shell :slight_smile:

2 Likes

Use /c instead of /k

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
3 Likes

try Shell.jl? GitHub - innerlee/Shell.jl: Run shell commands becomes easy in Julia!

2 Likes

Seems like basically we’d need a PR that reverts #30022 (to re-include busybox on Windows) and then adds it to the command path.

If one actually needs an echo program on Windows, I got one when installing git, C:\Program Files\Git\usr\bin\echo.exe.

1 Like
1 Like

Well, echo was just a mnwe (minimum non working example) and not the motivation for my question. But, good to know!

1 Like

Hi Sudete, you nailed my problem: I was assuming run sends command to a shell, and from there, I was floored. :+1:

I will!