Std process command usage

Hi all,

what's the difference of Command::new exe_path vs Command::new sh -c exe_path
which one should I use

Thanks,

Command::new("ls")
    .status()
    .expect("ls command failed to start");

Command::new("sh")
    .arg("-c")
    .arg("ls")
    .status()
    .expect("ls command failed to start");

The former runs the program directly. It either works or it doesn't, pretty much.

The latter tells a shell to run a command. That command might resolve to the same program, or maybe the shell has an alias that takes precedence, or there's a different one in the search path, or it's actually a built-in command and so it will ignore the executable, or ...

You should probably prefer the first one if possible, since it's simpler and is thus less likely to fail.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.