On Tue, 1 Jul 2025, 08:59 Zachary Santer, <zsan...@gmail.com> wrote: > It appears bash has made the parser more complex by selectively > removing the pre-expansion step for certain parts of expressions. > That's bad because the rules for when that happens are undocumented > [...] >
By far the simplest approach for users to understand would be to: (1) parse expressions inside identified numeric contexts at the same time as the surrounding code (rather than simply slurping up text which is re-parsed when the outer expansion is performed); and ... It seems like the pre-expansion step could be removed for only what > appears between [ and ] in arithmetic contexts and you'd have what you > need. At whatever later stage where bash knows if it's dealing with an > indexed or an associative array element, that content itself could be > evaluated in an arithmetic context or not. > (2) parse but defer all $ and `` expansions inside an identified numeric context until the applicable subexpression is evaluated. Expansion of associative array keys would also be deferred, but parsed as a simple concatenation of text components. "Inside an identified numeric context" would ideally include inside (()), $(()), $[], numeric array indexes, and assignment to a variable with the -i attribute. Of course, this would not be strictly compatible with POSIX, and it would make it more complex to write $operator-style expressions, so it would be to be gated by a shopt or compat setting. It would also mean that declare -i and declare -A would affect how subsequent code is parsed, which makes me want some equivalent of Perl's “BEGIN” keywords to expedite the effect of declarations found in the middle of any compound statement. A less unpredictable alternative would be to have some syntactic marker that a subscript should be parsed as associative rather than as a numeric expression. In any other language this is obvious. e.g. in PHP: $foo[$bar+$zot] # clearly numeric index $foo["$bar+$zot"] # clearly string lookup Clearly we can't use quotes like this in the Shell, so we would need some other indicator, such as ${map[[$key]]} or ${map{$key}} or ${map.$key} -- feel free to make up your own suggestion if you don't like any of these. You need to do something complex like this, or an associative array > key like ']' Preemptive textual expansion within expressions and subscripts will never be satisfactory, particularly when it interacts with ongoing changes to implicit vs explicit quoting rules. There will always be corner cases where user-supplied data will break how expressions are parsed, or leave gaping security holes, or both. Do my suggestions above sound like a reasonable future roadmap for Bash (when not in POSIX mode), or are there going to be so many naysayers that any effort to implement this will be a waste of time? -Martin