bash commands in rust

To run bash commands in Rust, you can use the std::process::Command module.

Here is an example of how you can run bash commands in Rust:

main.rs
use std::process::Command;

fn main() {
    let output = Command::new("sh")
        .arg("-c")
        .arg("ls -l")
        .output()
        .expect("failed to execute process");

    println!("status: {}", output.status);
    println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
    println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
}
365 chars
14 lines

In this example, the Command struct is used to spawn the sh shell and execute the ls -l command. The output() function is used to capture the output of the command.

You can replace the "ls -l" command with any bash command you want to execute.

Remember to add use std::process::Command; at the top of your Rust file to use the Command module.

related categories

gistlibby LogSnag