How would I output some text onto the console using Unix commands?

Using Arch-based Linux system, I have written this code.

use std::process::Command;

fn main()
{
    let mut output = Command::new("sh");
    output.arg("-c").arg("echo hello");
    output.output().expect("Error!");
}

So when I execute it I am not getting any output at all. Is there a reason behind htis?

Command.output() captures the process output and returns it as a Vec<u8> that you can use inside your program. If you don’t need the output and want to let the program access the console, use Command.status instead:

use std::process::Command;

fn main()
{
    let mut output = Command::new("sh");
    output.arg("-c").arg("echo hello");
    output.status().expect("Error!");
}
1 Like

Ah I see that makes sense, thanks :slight_smile:

Just curious to know a few things, let mut output = Command::new("sh"); So what does "sh" even mean?

And with output.arg("-c") what does the "-c" even mean?

sh is the POSIX-standard command interpreter; it’s usually the program that runs automatically when you open a new terminal. -c tells it to interpret the next argument as an explicit command instead of the filename of a script that it should run.

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.