Function shell_words::join

source ·
pub fn join<I, S>(words: I) -> String
where I: IntoIterator<Item = S>, S: AsRef<str>,
Expand description

Joins arguments into a single command line suitable for execution in Unix shell.

Each argument is quoted using quote to preserve its literal meaning when parsed by Unix shell.

Note: This function is essentially an inverse of split.

§Examples

Logging executed commands in format that can be easily copied and pasted into an actual shell:

fn execute(args: &[&str]) {
    use std::process::Command;
    println!("Executing: {}", shell_words::join(args));
    Command::new(&args[0])
        .args(&args[1..])
        .spawn()
        .expect("failed to start subprocess")
        .wait()
        .expect("failed to wait for subprocess");
}

execute(&["python", "-c", "print('Hello world!')"]);