Skip to content

Commit 87158ea

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 3b61c38 commit 87158ea

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
@@ -7704,15 +7704,18 @@ impl Interpreter {
77047704
s.to_string()
77057705
}
77067706

7707-
/// Expand an associative array key with full word expansion.
7708-
/// Unlike `expand_variable_or_literal`, this handles command substitutions
7709-
/// (`$(...)`, backticks) and all other expansion types. (Issue #872)
7707+
/// Fully expand an associative array key, including command substitutions.
7708+
/// Falls back to `expand_variable_or_literal` for keys without `$(` or backtick.
77107709
async fn expand_assoc_key(&mut self, s: &str) -> Result<String> {
7711-
if s.contains('$') || s.contains('`') {
7712-
let word = crate::parser::Parser::parse_word_string(s);
7710+
if s.contains("$(") || s.contains('`') {
7711+
let word = Parser::parse_word_string_with_limits(
7712+
s,
7713+
self.limits.max_ast_depth,
7714+
self.limits.max_parser_operations,
7715+
);
77137716
self.expand_word(&word).await
77147717
} else {
7715-
Ok(s.to_string())
7718+
Ok(self.expand_variable_or_literal(s))
77167719
}
77177720
}
77187721

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ b=2
173173
c=3
174174
### end
175175

176+
### assoc_key_command_substitution
177+
declare -A m=()
178+
m["$(echo hello)"]="world"
179+
echo "count: ${#m[@]}"
180+
for k in "${!m[@]}"; do echo "key=[$k] val=[${m[$k]}]"; done
181+
### expect
182+
count: 1
183+
key=[hello] val=[world]
184+
### end
185+
176186
### assoc_iteration
177187
declare -A m
178188
m[a]="1"

0 commit comments

Comments
 (0)