Function shellexpand::env

source ·
pub fn env<SI>(input: &SI) -> Result<Cow<'_, str>, LookupError<VarError>>
where SI: AsRef<str> + ?Sized,
Expand description

Performs the environment expansion using the default system context.

This function delegates to env_with_context(), using the default system source for environment variables, namely the std::env::var() function.

Note that variable lookup of unknown variables will fail with an error instead of, for example, replacing the offending variables with an empty string. The author thinks that such behavior is more useful than the other ones. If you need something else, use env_with_context() or env_with_context_no_errors() with an appropriate context function.

§Examples

use std::env;

// make sure that some environment variables are set
env::set_var("X", "x value");
env::set_var("Y", "y value");

// Known variables are expanded
assert_eq!(
    shellexpand::env("begin/$X/${Y}s/end").unwrap(),
    "begin/x value/y values/end"
);

// Unknown variables result in an error
assert_eq!(
    shellexpand::env("begin/$Z/end"),
    Err(shellexpand::LookupError {
        var_name: "Z".into(),
        cause: env::VarError::NotPresent
    })
);