Was "bash tries to parse comsub in quoted PE pattern" On Wed, Oct 18, 2023 at 8:19 AM Zachary Santer <zsan...@gmail.com> wrote: > > In Bash 5.2: > $ array=( zero one two three four five six ) > $ printf '%s\n' "${array["{2..6..2}"]}" > two > four > six > $ printf '%s\n' "${array[{2..6..2}]}" > -bash: {2..6..2}: syntax error: operand expected (error token is "{2..6..2}") > $ printf '%s\n' "${array["2 4 6"]}" > -bash: 2 4 6: syntax error in expression (error token is "4 6") > $ printf '%s\n' "${array[2 4 6]}" > -bash: 2 4 6: syntax error in expression (error token is "4 6") > $ printf '%s\n' "${array[2,4,6]}" > six > $ indices=(2 4 6) > $ printf '%s\n' "${array[${indices[@]}]}" > -bash: 2 4 6: syntax error in expression (error token is "4 6") > $ printf '%s\n' "${array[${indices[*]}]}" > -bash: 2 4 6: syntax error in expression (error token is "4 6")
My mind returns to this nonsense, as I find a use for it. Imagine this functionality: $ array=( zero one two three four five six ) $ printf '%s\n' "${array[@]( 1 5 )}" one five $ printf '%s\n' "${array[*]( 1 5 )}" one five $ indices_array=( 6 2 ) $ printf '%s\n' "${array[@]( "${indices_array[@]}" )}" six two $ indices_scalar='-7 -4' $ printf '%s\n' "${array[@]( ${indices_scalar} )}" zero three $ scalar='0123456' $ printf '%s\n' "${scalar( 1 5 )}" 15 $ printf '%s\n' "${scalar( "${indices_array[@]}" )}" 62 $ printf '%s\n' "${scalar( ${indices_scalar} )}" 03 The ( ) within the parameter expansion would be roughly analogous to the right hand side of a compound assignment statement for an indexed array. The values found therein would be taken as the indices of array elements or characters to expand. Trying to set indices for the indices, i.e. "${array[@]( [10]=1 [20]=5 )}", wouldn't make any sense, though, so not quite the same construct. This could be useful with associative arrays as well, unlike "${assoc[@]:offset:length}". I've repeatedly found myself in situations where I had to construct a whole new array out of not-necessarily-contiguous elements of another array, just to be able to expand that array somewhere. It would've been nicer to just use a set of indices directly. I'm now in a situation where I already have the set of indices and I have to loop over them to construct the array I need. I present this as also applying to characters within a scalar variable, just to be consistent with ${var:offset:length}, which applies to both scalars and arrays. Maybe that could be useful. I don't know. Does this functionality seem valuable to others? Sorry for being such an ideas guy. Zack