Command always panicks

How to fix it?

    Command::new("echo").arg("hello world").output().expect("Unable to run cmd");

this always returns:

thread 'main' (7448) panicked at src\main.rs:9:44:
Unable to run cmd: Error { kind: NotFound, message: "program not found" }
stack backtrace:
   0: std::panicking::panic_handler
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\panicking.rs:698
   1: core::panicking::panic_fmt
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\core\src\panicking.rs:80
   2: core::result::unwrap_failed
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\core\src\result.rs:1862
   3: bbrotserver::main
   4: bbrotserver::main
   5: bbrotserver::main
   6: std::rt::lang_start_internal::closure$0
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\rt.rs:175
   7: std::panicking::catch_unwind::do_call
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\panicking.rs:590
   8: std::panicking::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\panicking.rs:553
   9: std::panic::catch_unwind
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\
                                                                               \panic.rs:359
  10: std::rt::lang_start_internal
             at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library\std\src\
                                                                               \rt.rs:171
  11: main
  12: invoke_main
             at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.
                                                                               .inl:78
  13: __scrt_common_main_seh
             at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.
                                                                               .inl:288
  14: BaseThreadInitThunk
  15: RtlUserThreadStart
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose bac
                                                                               cktrace.

on windows, echo is a shell built-in command, there's no echo.exe binary, you need to call cmd.exe with the /c option instead:

    Command::new("cmd").args(["/c", "echo \"hello world\""]).output().expect("Unable to run cmd");
1 Like

okay. i am not getting the error now. at the same time, echo command is not working as well

please be more specific, what's the output you get, and what's your expected result?

without further information, I guess it's probably caused by the archaic command line parsing and quotation behavior of cmd.exe. I'm not on Windows, have fun and good luck playing with that.

It is just not printing anything.
the command is analogous to this.


if you notice it, a new cmd instance is spawned inside an existing cmd instance. and it seems that cmd.exe does not accept any arguments like how we expect.

of course it doesn't, because stdout is redirected and captured by the parent process, that's the whole point of Command::output().

if you don't need to capture stdout and just want it to inherit the parent process's, then use Command::status() instead of Command::output() (you can manually modify the behavior via Command::stdout() if you need more control).

It doesn't answer the question. If you see the screenshot, windows cmd itself is not printing the expected output. neither does it execute any command that is given in arguments.

cmd.exe start 1.txt this is supposed to open 1.txt present in current directory with default program.
but it won't work because of the issue i explained. it is not related to stdout.

you forgot the /c command line flag.

1 Like