I am trying to use Command::process to spawn some processes and to get started I want to check I can get a simple command like echo
to work correctly. I want to take some user input and then spawn a process to echo this back to stdout, so that I can verify that I get a response like:
> echo this!
echo this! <-- my echoed text
Currently, I am trying to do this like this:
use std::io;
use std::process::Command;
fn main() {
let mut some_input = String::new();
io::stdin()
.read_line(&mut some_input)
.unwrap();
let command_example = Command::new("echo")
.arg(&some_input)
.output()
.unwrap();
let expected_output = format!("{}\n", &some_input);
assert_eq!(expected_output.as_bytes(), command_example.stdout.as_slice());
}
The assert
block is passing, so I can see that the output of the command ends up in the struct's stout buffer. However, when I run this in the terminal, there is no output, the program just ends without printing anything back to stdout. What am I doing wrong that this code doesn't result in something being printed to the stdout?
From the documentation:
Executes the command as a child process, waiting for it to finish and collecting all of its output.
Thus, you obtain all the standard output of the process in your command_example
variable.
Nothing appears in the terminal, because this was collected.
Alternatively, instead of .output()
, you can use .status()
in order to run the process without capturing its standard output; it will then show up in the terminal.