Expand description

Macros to run bash scripts inline in Rust

In many cases it’s convenient to run child processes, particularly via Unix shell script. Writing the Rust code to use std::process::Command directly will get very verbose quickly. You can generate a script “manually” by using e.g. format!() but there are some important yet subtle things to get right, such as dealing with quoting issues.

This macro takes Rust variable names at the start that are converted to a string (quoting as necessary) and bound into the script as bash variables.

Further, the generated scripts use “bash strict mode” by default, i.e. set -euo pipefail.

use sh_inline::*;
let foo = "variable with spaces";
bash!(r#"test "${foo}" = 'variable with spaces'"#, foo)?;

This generates and executes bash script as follows:

set -euo pipefail
foo="variable with spaces"
test ${foo} = 'variable with spaces'

xshell is a crate that does not depend on bash, and also supports inline formatting.


pub use cap_std_ext;
pub use cap_std_ext::cap_std;


Execute a fragment of Bash shell script, returning an error if the subprocess exits unsuccessfully. This is intended as a convenience macro for the common case of wanting to just propagate errors. The returned error type is std::io::Error.

Create a Command object that will execute a fragment of (Bash) shell script in “strict mode”, i.e. with set -euo pipefail. The first argument is the script, and additional arguments should be Rust variable identifiers. The provided Rust variables will become shell script variables with their values quoted.

Execute a fragment of Bash shell script with the specified working directory.