pub struct Command { /* private fields */ }
Expand description
A process builder, providing fine-grained control over how a new process should be spawned.
A default configuration can be
generated using Command::new(program)
, where program
gives a path to the
program to be executed. Additional builder methods allow the configuration
to be changed (for example, by adding arguments) prior to spawning:
use std::process::Command;
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};
let hello = output.stdout;
Command
can be reused to spawn multiple processes. The builder methods
change the command without needing to immediately spawn the process.
use std::process::Command;
let mut echo_hello = Command::new("sh");
echo_hello.arg("-c").arg("echo hello");
let hello_1 = echo_hello.output().expect("failed to execute process");
let hello_2 = echo_hello.output().expect("failed to execute process");
Similarly, you can call builder methods after spawning a process and then spawn a new process with the modified settings.
use std::process::Command;
let mut list_dir = Command::new("ls");
// Execute `ls` in the current directory of the program.
list_dir.status().expect("process failed to execute");
println!();
// Change `ls` to execute in the root directory.
list_dir.current_dir("/");
// And then execute `ls` again but in the root directory.
list_dir.status().expect("process failed to execute");
Implementations§
source§impl Command
impl Command
1.0.0 · sourcepub fn new<S: AsRef<OsStr>>(program: S) -> Command
pub fn new<S: AsRef<OsStr>>(program: S) -> Command
Constructs a new Command
for launching the program at
path program
, with the following default configuration:
- No arguments to the program
- Inherit the current process’s environment
- Inherit the current process’s working directory
- Inherit stdin/stdout/stderr for
spawn
orstatus
, but create pipes foroutput
Builder methods are provided to change these defaults and otherwise configure the process.
If program
is not an absolute path, the PATH
will be searched in
an OS-defined way.
The search path to be used may be controlled by setting the
PATH
environment variable on the Command,
but this has some implementation limitations on Windows
(see issue #37519).
§Platform-specific behavior
Note on Windows: For executable files with the .exe extension, it can be omitted when specifying the program for this Command. However, if the file has a different extension, a filename including the extension needs to be provided, otherwise the file won’t be found.
§Examples
§Caveats
Command::new
is only intended to accept the path of the program. If you pass a program
path along with arguments like Command::new("ls -l").spawn()
, it will try to search for
ls -l
literally. The arguments need to be passed separately, such as via arg
or
args
.
1.0.0 · sourcepub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command
Adds an argument to pass to the program.
Only one argument can be passed per use. So instead of:
usage would be:
To pass multiple arguments see args
.
Note that the argument is not passed through a shell, but given literally to the program. This means that shell syntax like quotes, escaped characters, word splitting, glob patterns, variable substitution, etc. have no effect.
On Windows, use caution with untrusted inputs. Most applications use the
standard convention for decoding arguments passed to them. These are safe to
use with arg
. However, some applications such as cmd.exe
and .bat
files
use a non-standard way of decoding arguments. They are therefore vulnerable
to malicious input.
In the case of cmd.exe
this is especially important because a malicious
argument can potentially run arbitrary shell commands.
See Windows argument splitting for more details
or raw_arg
for manually implementing non-standard argument encoding.
§Examples
1.0.0 · sourcepub fn args<I, S>(&mut self, args: I) -> &mut Command
pub fn args<I, S>(&mut self, args: I) -> &mut Command
Adds multiple arguments to pass to the program.
To pass a single argument see arg
.
Note that the arguments are not passed through a shell, but given literally to the program. This means that shell syntax like quotes, escaped characters, word splitting, glob patterns, variable substitution, etc. have no effect.
On Windows, use caution with untrusted inputs. Most applications use the
standard convention for decoding arguments passed to them. These are safe to
use with arg
. However, some applications such as cmd.exe
and .bat
files
use a non-standard way of decoding arguments. They are therefore vulnerable
to malicious input.
In the case of cmd.exe
this is especially important because a malicious
argument can potentially run arbitrary shell commands.
See Windows argument splitting for more details
or raw_arg
for manually implementing non-standard argument encoding.
§Examples
1.0.0 · sourcepub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
Inserts or updates an explicit environment variable mapping.
This method allows you to add an environment variable mapping to the spawned process or
overwrite a previously set value. You can use Command::envs
to set multiple environment
variables simultaneously.
Child processes will inherit environment variables from their parent process by default.
Environment variables explicitly set using Command::env
take precedence over inherited
variables. You can disable environment variable inheritance entirely using
Command::env_clear
or for a single key using Command::env_remove
.
Note that environment variable names are case-insensitive (but case-preserving) on Windows and case-sensitive on all other platforms.
§Examples
1.19.0 · sourcepub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
Inserts or updates multiple explicit environment variable mappings.
This method allows you to add multiple environment variable mappings to the spawned process
or overwrite previously set values. You can use Command::env
to set a single environment
variable.
Child processes will inherit environment variables from their parent process by default.
Environment variables explicitly set using Command::envs
take precedence over inherited
variables. You can disable environment variable inheritance entirely using
Command::env_clear
or for a single key using Command::env_remove
.
Note that environment variable names are case-insensitive (but case-preserving) on Windows and case-sensitive on all other platforms.
§Examples
use std::process::{Command, Stdio};
use std::env;
use std::collections::HashMap;
let filtered_env : HashMap<String, String> =
env::vars().filter(|&(ref k, _)|
k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
).collect();
Command::new("printenv")
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.env_clear()
.envs(&filtered_env)
.spawn()
.expect("printenv failed to start");
1.0.0 · sourcepub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command
Removes an explicitly set environment variable and prevents inheriting it from a parent process.
This method will remove the explicit value of an environment variable set via
Command::env
or Command::envs
. In addition, it will prevent the spawned child
process from inheriting that environment variable from its parent process.
After calling Command::env_remove
, the value associated with its key from
Command::get_envs
will be None
.
To clear all explicitly set environment variables and disable all environment variable
inheritance, you can use Command::env_clear
.
§Examples
1.0.0 · sourcepub fn env_clear(&mut self) -> &mut Command
pub fn env_clear(&mut self) -> &mut Command
Clears all explicitly set environment variables and prevents inheriting any parent process environment variables.
This method will remove all explicitly added environment variables set via Command::env
or Command::envs
. In addition, it will prevent the spawned child process from inheriting
any environment variable from its parent process.
After calling Command::env_clear
, the iterator from Command::get_envs
will be
empty.
You can use Command::env_remove
to clear a single mapping.
§Examples
1.0.0 · sourcepub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command
Sets the working directory for the child process.
§Platform-specific behavior
If the program path is relative (e.g., "./script.sh"
), it’s ambiguous
whether it should be interpreted relative to the parent’s working
directory or relative to current_dir
. The behavior in this case is
platform specific and unstable, and it’s recommended to use
canonicalize
to get an absolute program path instead.
§Examples
1.0.0 · sourcepub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
1.0.0 · sourcepub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
1.0.0 · sourcepub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command
1.0.0 · sourcepub fn spawn(&mut self) -> Result<Child>
pub fn spawn(&mut self) -> Result<Child>
Executes the command as a child process, returning a handle to it.
By default, stdin, stdout and stderr are inherited from the parent.
§Examples
1.0.0 · sourcepub fn output(&mut self) -> Result<Output>
pub fn output(&mut self) -> Result<Output>
Executes the command as a child process, waiting for it to finish and collecting all of its output.
By default, stdout and stderr are captured (and used to provide the resulting output). Stdin is not inherited from the parent and any attempt by the child process to read from the stdin stream will result in the stream immediately closing.
§Examples
use std::process::Command;
use std::io::{self, Write};
let output = Command::new("/bin/cat")
.arg("file.txt")
.output()
.expect("failed to execute process");
println!("status: {}", output.status);
io::stdout().write_all(&output.stdout).unwrap();
io::stderr().write_all(&output.stderr).unwrap();
assert!(output.status.success());
1.0.0 · sourcepub fn status(&mut self) -> Result<ExitStatus>
pub fn status(&mut self) -> Result<ExitStatus>
Executes a command as a child process, waiting for it to finish and collecting its status.
By default, stdin, stdout and stderr are inherited from the parent.
§Examples
use std::process::Command;
let status = Command::new("/bin/cat")
.arg("file.txt")
.status()
.expect("failed to execute process");
println!("process finished with: {status}");
assert!(status.success());
1.57.0 · sourcepub fn get_program(&self) -> &OsStr
pub fn get_program(&self) -> &OsStr
Returns the path to the program that was given to Command::new
.
§Examples
1.57.0 · sourcepub fn get_args(&self) -> CommandArgs<'_> ⓘ
pub fn get_args(&self) -> CommandArgs<'_> ⓘ
Returns an iterator of the arguments that will be passed to the program.
This does not include the path to the program as the first argument;
it only includes the arguments specified with Command::arg
and
Command::args
.
§Examples
1.57.0 · sourcepub fn get_envs(&self) -> CommandEnvs<'_> ⓘ
pub fn get_envs(&self) -> CommandEnvs<'_> ⓘ
Returns an iterator of the environment variables explicitly set for the child process.
Environment variables explicitly set using Command::env
, Command::envs
, and
Command::env_remove
can be retrieved with this method.
Note that this output does not include environment variables inherited from the parent process.
Each element is a tuple key/value pair (&OsStr, Option<&OsStr>)
. A None
value
indicates its key was explicitly removed via Command::env_remove
. The associated key for
the None
value will no longer inherit from its parent process.
An empty iterator can indicate that no explicit mappings were added or that
Command::env_clear
was called. After calling Command::env_clear
, the child process
will not inherit any environment variables from its parent process.
§Examples
Trait Implementations§
1.0.0 · source§impl CommandExt for Command
Available on Unix only.
impl CommandExt for Command
source§fn uid(&mut self, id: u32) -> &mut Command
fn uid(&mut self, id: u32) -> &mut Command
setuid
call in the child process. Failure in the setuid
call will cause the spawn to fail. Read moresource§fn gid(&mut self, id: u32) -> &mut Command
fn gid(&mut self, id: u32) -> &mut Command
uid
, but sets the group ID of the child process. This has
the same semantics as the uid
field.source§fn groups(&mut self, groups: &[u32]) -> &mut Command
fn groups(&mut self, groups: &[u32]) -> &mut Command
setgroups
#90747)setgroups
call in the child process.source§unsafe fn pre_exec<F>(&mut self, f: F) -> &mut Command
unsafe fn pre_exec<F>(&mut self, f: F) -> &mut Command
exec
function is
invoked. Read moresource§impl CommandExt for Command
Available on Linux only.
impl CommandExt for Command
1.16.0 · source§impl CommandExt for Command
Available on Windows only.
impl CommandExt for Command
source§fn creation_flags(&mut self, flags: u32) -> &mut Command
fn creation_flags(&mut self, flags: u32) -> &mut Command
source§fn show_window(&mut self, cmd_show: u16) -> &mut Command
fn show_window(&mut self, cmd_show: u16) -> &mut Command
windows_process_extensions_show_window
#127544)wShowWindow
of STARTUPINFO that is passed to CreateProcess
.
Allowed values are the ones listed in
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindowsource§fn force_quotes(&mut self, enabled: bool) -> &mut Command
fn force_quotes(&mut self, enabled: bool) -> &mut Command
windows_process_extensions_force_quotes
#82227)"
) characters. Read moresource§fn raw_arg<S: AsRef<OsStr>>(&mut self, raw_text: S) -> &mut Command
fn raw_arg<S: AsRef<OsStr>>(&mut self, raw_text: S) -> &mut Command
source§fn async_pipes(&mut self, always_async: bool) -> &mut Command
fn async_pipes(&mut self, always_async: bool) -> &mut Command
windows_process_extensions_async_pipes
#98289)process::Command
creates pipes, request that our side is always async. Read moresource§unsafe fn raw_attribute<T: Copy + Send + Sync + 'static>(
&mut self,
attribute: usize,
value: T,
) -> &mut Command
unsafe fn raw_attribute<T: Copy + Send + Sync + 'static>( &mut self, attribute: usize, value: T, ) -> &mut Command
windows_process_extensions_raw_attribute
#114854)1.0.0 · source§impl Debug for Command
impl Debug for Command
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Format the program and arguments of a Command for display. Any non-utf8 data is lossily converted using the utf8 replacement character.
The default format approximates a shell invocation of the program along with its arguments. It does not include most of the other command properties. The output is not guaranteed to work (e.g. due to lack of shell-escaping or differences in path resolution). On some platforms you can use the alternate syntax to show more fields.
Note that the debug implementation is platform-specific.