expand_variables_in_string

Function expand_variables_in_string 

Source
pub fn expand_variables_in_string(
    input: &str,
    shell_state: &mut ShellState,
) -> String
Expand description

Expands shell-style variables, command substitutions, arithmetic expressions, and backtick substitutions inside a string.

This function processes $VAR and positional/special parameters ($1, $?, $#, $*, $@, $$, $0), command substitutions using $(...) and backticks, and arithmetic expansions using $((...)), producing the resulting string with substitutions applied. Undefined numeric positional parameters and the documented special parameters expand to an empty string; other undefined variable names are left as literal $NAME. Arithmetic evaluation errors are rendered as an error message (colorized when the shell state enables colors). Command substitutions are parsed and executed using the current shell state; on failure the original substitution text is preserved.

§Examples

use rush_sh::ShellState;
use rush_sh::executor::expand_variables_in_string;
// assume `shell_state` is a mutable ShellState with VAR=hello
let mut shell_state = ShellState::new();
shell_state.set_var("VAR", "hello".to_string());
let input = "Value:$VAR";
let out = expand_variables_in_string(input, &mut shell_state);
assert_eq!(out, "Value:hello");

§Errors

Returns Err if input cannot be tokenized