Skip to main content

cmd

Macro cmd 

Source
macro_rules! cmd {
    ($cmd:expr) => { ... };
    ($fmt:expr, $($arg:expr),+ $(,)?) => { ... };
}
Expand description

The cmd! macro for ergonomic shell command execution

This macro provides a similar experience to JavaScript’s $ tagged template literal. Values interpolated into the command are automatically quoted for shell safety.

Note: Consider using the shorter s! or sh! aliases for more concise code.

§Examples

use command_stream::s;

// Simple command (returns a future that can be awaited)
let result = s!("echo hello").await?;

// With string interpolation
let name = "world";
let result = s!("echo hello {}", name).await?;

// With multiple values
let src = "source.txt";
let dst = "dest.txt";
let result = s!("cp {} {}", src, dst).await?;

// Values with special characters are automatically quoted
let filename = "file with spaces.txt";
let result = s!("cat {}", filename).await?; // Safely handles spaces

§Safety

All interpolated values are automatically quoted using shell-safe quoting, preventing command injection attacks.