Why can't I execute some commands from Process? Why can't I execute some commands from Process? dart dart

Why can't I execute some commands from Process?


Because echo is not a program found on the computer, it is a shell command. Command::new takes in an executable to run as a sub-process.

If you are on windows you can use

Command::new("cmd")    .args(&["/C", "echo hello!"])    .spawn()    .expect("echo command failed to start");

to run the CMD program and /C to pass in a command, in this case echo.

On linux you can use the bash executable.

Command::new("bash")    .args(&["-c", "echo hello"])    .spawn()    .expect("echo command failed to start");

See the rust docs for std::process::Command


You need to set the runInShell param to true, like:

  var r = await Process.run('echo', ['hello'], runInShell: true);  print(r.stdout); //Prints 'hello'

From the dart api

If runInShell is true, the process will be spawned through a system shell. On Linux and OS X, /bin/sh is used, while %WINDIR%\system32\cmd.exe is used on Windows.