Skip to content

Commit f18c976

Browse files
committed
fix(interpreter): expand command substitutions in assoc array keys
Associative array assignments where the key is a command substitution (e.g. m["$(echo hello)"]="world") silently produced an empty key. Add async expand_assoc_key() that parses the subscript as a full Word and expands it with expand_word() when it contains $( or backtick. Closes #872
1 parent a4e09c8 commit f18c976

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

crates/bashkit/src/interpreter/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,15 +7714,18 @@ impl Interpreter {
77147714
s.to_string()
77157715
}
77167716

7717-
/// Expand an associative array key with full word expansion.
7718-
/// Unlike `expand_variable_or_literal`, this handles command substitutions
7719-
/// (`$(...)`, backticks) and all other expansion types. (Issue #872)
7717+
/// Fully expand an associative array key, including command substitutions.
7718+
/// Falls back to `expand_variable_or_literal` for keys without `$(` or backtick.
77207719
async fn expand_assoc_key(&mut self, s: &str) -> Result<String> {
7721-
if s.contains('$') || s.contains('`') {
7722-
let word = crate::parser::Parser::parse_word_string(s);
7720+
if s.contains("$(") || s.contains('`') {
7721+
let word = Parser::parse_word_string_with_limits(
7722+
s,
7723+
self.limits.max_ast_depth,
7724+
self.limits.max_parser_operations,
7725+
);
77237726
self.expand_word(&word).await
77247727
} else {
7725-
Ok(s.to_string())
7728+
Ok(self.expand_variable_or_literal(s))
77267729
}
77277730
}
77287731

crates/bashkit/tests/spec_cases/bash/assoc-arrays.test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ result: [a
202202
b]
203203
### end
204204

205+
### assoc_key_command_substitution
206+
declare -A m=()
207+
m["$(echo hello)"]="world"
208+
echo "count: ${#m[@]}"
209+
for k in "${!m[@]}"; do echo "key=[$k] val=[${m[$k]}]"; done
210+
### expect
211+
count: 1
212+
key=[hello] val=[world]
213+
### end
214+
205215
### assoc_iteration
206216
declare -A m
207217
m[a]="1"

0 commit comments

Comments
 (0)