diff --git a/brush-core/src/builtins/unset.rs b/brush-core/src/builtins/unset.rs index 94ac0052..da3b1c67 100644 --- a/brush-core/src/builtins/unset.rs +++ b/brush-core/src/builtins/unset.rs @@ -60,11 +60,14 @@ impl builtins::Command for UnsetCommand { context.shell.env.unset(name.as_str())? } brush_parser::word::Parameter::NamedWithIndex { name, index } => { - // TODO: Evaluate index? + // First evaluate the index expression. + let index_as_expr = brush_parser::arithmetic::parse(index.as_str())?; + let evaluated_index = context.shell.eval_arithmetic(index_as_expr).await?; + context .shell .env - .unset_index(name.as_str(), index.as_str())? + .unset_index(name.as_str(), evaluated_index.to_string().as_str())? } brush_parser::word::Parameter::NamedWithAllIndices { name: _, diff --git a/brush-parser/src/word.rs b/brush-parser/src/word.rs index bb965db1..f426e9fb 100644 --- a/brush-parser/src/word.rs +++ b/brush-parser/src/word.rs @@ -653,7 +653,7 @@ peg::parser! { p:special_parameter() { Parameter::Special(p) } / non_posix_extensions_enabled() p:variable_name() "[@]" { Parameter::NamedWithAllIndices { name: p.to_owned(), concatenate: false } } / non_posix_extensions_enabled() p:variable_name() "[*]" { Parameter::NamedWithAllIndices { name: p.to_owned(), concatenate: true } } / - non_posix_extensions_enabled() p:variable_name() "[" index:$((!"]" [_])*) "]" {? + non_posix_extensions_enabled() p:variable_name() "[" index:$(arithmetic_word(<"]">)) "]" {? Ok(Parameter::NamedWithIndex { name: p.to_owned(), index: index.to_owned() }) } / p:variable_name() { Parameter::Named(p.to_owned()) } diff --git a/brush-shell/tests/cases/arrays.yaml b/brush-shell/tests/cases/arrays.yaml index 21abb97d..b548d35f 100644 --- a/brush-shell/tests/cases/arrays.yaml +++ b/brush-shell/tests/cases/arrays.yaml @@ -270,3 +270,8 @@ cases: stdin: | declare -A myarray=([0]="default" ["a"]="other") echo "Value: ${myarray[$non_existent_var]}" + + - name: "Nested array reference" + stdin: | + x=(2 3 4) + echo ${x[${x[0]}]} diff --git a/brush-shell/tests/cases/builtins/unset.yaml b/brush-shell/tests/cases/builtins/unset.yaml index 1081c20a..dfbae9fd 100644 --- a/brush-shell/tests/cases/builtins/unset.yaml +++ b/brush-shell/tests/cases/builtins/unset.yaml @@ -48,12 +48,23 @@ cases: declare -p myarray - name: "Unset array element with interesting expression" - known_failure: true # TODO: case needs implementing stdin: | declare -a myarray=(a b c d e) + echo "Initial array:" + declare -p myarray + echo + + i=4 + unset myarray[i] + echo "After removing element at index 4:" + declare -p myarray + echo + unset myarray[6/2] + echo "After removing element at index 3:" declare -p myarray + echo - name: "Unset local in same function" stdin: |