expand

Function expand 

Source
pub fn expand<S: AsRef<str>>(
    shell_string: &str,
    env_vars: HashMap<S, S>,
) -> Result<String, String>
Expand description

Perform shell expansion on shell_string as if it were within double quotes, using env_vars to resolve variables. This includes parameter expansion, arithmetic expansion, and quote removal.

System environment variables can be used for expansion by using the util::env_map function.

Command substitutions like $(echo foo) aren’t supported to avoid running arbitrary code. To support those, use an interpreter from the expand module.

If the input string has invalid syntax, Err is returned.

§Example

use std::env;
use husk::{shell, util};

env::set_var("NAME", "Foo Bar");
let env_map = util::env_map();
let expanded_string = shell::expand("Hello, ${NAME}!", env_map).unwrap();
assert_eq!(expanded_string, "Hello, Foo Bar!");