Associative array keys are not reusable in (( command

2021-01-08 Thread Oğuz
See: $ declare -A assoc=($'\n\'' 42) $ for key in "${!assoc[@]}"; do (( assoc[$key]++ )); done bash: ((: assoc[ ']++ : bad array subscript (error token is "assoc[ ']++ ") $ $ (( assoc[${key@Q}]++ )) bash: ((: assoc[$'\n\'']++ : bad array subscript (error token is "a

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Koichi Murase
This topic actually pops up occasionally. FYI, you can also see the following discussions: https://lists.gnu.org/archive/html/bug-bash/2014-06/msg3.html https://lists.gnu.org/archive/html/bug-bash/2014-10/msg00154.html https://lists.gnu.org/archive/html/bug-bash/2015-02/msg00066.html 2021年1月

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Oğuz
On Fri, Jan 8, 2021 at 2:14 PM Koichi Murase wrote: > This topic actually pops up occasionally. FYI, you can also see the > following discussions: > > https://lists.gnu.org/archive/html/bug-bash/2014-06/msg3.html > https://lists.gnu.org/archive/html/bug-bash/2014-10/msg00154.html > https://l

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Koichi Murase
2021年1月8日(金) 20:07 Oğuz : > On Fri, Jan 8, 2021 at 2:14 PM Koichi Murase wrote: >> There is no reason to introduce a different expansion rule of >> `((...))' from that of `$((...))'. > > But there already is a different expansion rule. While `(( > assoc['$key']++ ))' works, `: $(( assoc['$key']++

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Chet Ramey
On 1/8/21 5:20 AM, Oğuz wrote: See: $ declare -A assoc=($'\n\'' 42) $ for key in "${!assoc[@]}"; do (( assoc[$key]++ )); done bash: ((: assoc[ ']++ : bad array subscript (error token is "assoc[ ']++ ") $ $ (( assoc[${key@Q}]++ )) bash: ((: assoc[$'\n\'']++

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Oğuz
8 Ocak 2021 Cuma tarihinde Chet Ramey yazdı: > On 1/8/21 5:20 AM, Oğuz wrote: > >> See: >> >> $ declare -A assoc=($'\n\'' 42) >> $ for key in "${!assoc[@]}"; do (( assoc[$key]++ )); done >> bash: ((: assoc[ >> ']++ : bad array subscript (error token is "assoc[ >> ']++ ")

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Chet Ramey
On 1/8/21 10:24 AM, Oğuz wrote: 8 Ocak 2021 Cuma tarihinde Chet Ramey > yazdı: On 1/8/21 5:20 AM, Oğuz wrote: See:      $ declare -A assoc=($'\n\'' 42)      $ for key in "${!assoc[@]}"; do (( assoc[$key]++ )); done      bash:

Re: Associative array keys are not reusable in (( command

2021-01-08 Thread Ilkka Virta
On Fri, Jan 8, 2021 at 5:44 PM Chet Ramey wrote: > On 1/8/21 10:24 AM, Oğuz wrote: > > This situation is why bash-5.0 introduced the `assoc_expand_once' > option. > > But it allows arbitrary command injection. > > If you want to run array keys through word expansions, this is one > potential

Re: Associative array keys are not reusable in (( command

2021-01-10 Thread Chet Ramey
On 1/8/21 10:24 AM, Oğuz wrote: 8 Ocak 2021 Cuma tarihinde Chet Ramey > yazdı: On 1/8/21 5:20 AM, Oğuz wrote: See:      $ declare -A assoc=($'\n\'' 42)      $ for key in "${!assoc[@]}"; do (( assoc[$key]++ )); done      bash:

Re: Associative array keys are not reusable in (( command

2021-01-10 Thread Oğuz
10 Ocak 2021 Pazar tarihinde Chet Ramey yazdı: > So you have got (( assoc[x],b[$(uname >&2)] )), and the shell does exactly > what you think it does with that. > > This is a valid arithmetic expression. The `assoc' is marked as having its > subscript expanded once, since it has. The `b' is not, b

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Chet Ramey
On 1/10/21 2:55 PM, Oğuz wrote: If I don't know the key beforehand, like if I read it from a file or the like, this becomes a security issue. That is my concern, `assoc_expand_once' doesn't help in that situation. If you're taking input that you haven't verified and putting it into an expansi

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Greg Wooledge
On Mon, Jan 11, 2021 at 09:07:08AM -0500, Chet Ramey wrote: > On 1/10/21 2:55 PM, Oğuz wrote: > > > If I don't know the key beforehand, like if I read it from a file or the > > like, this becomes a security issue. That is my concern, > > `assoc_expand_once' doesn't help in that situation. > > If

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Chet Ramey
> So what I'm hearing is: We hear what we choose, I suppose. What semantics would you choose to change? -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Léa Gris
Declare an integer associative array instead: echo "$BASH_VERSION" 5.0.17(1)-release declare -Ai aa x='y[$(date >&2)0]' aa[$x]=1 declare -p aa declare -Ai aa=(["y[\$(date >&2)0]"]="1" ) aa[$x]+=1 declare -p aa declare -Ai aa=(["y[\$(date >&2)0]"]="2" ) -- Léa Gris

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Léa Gris
Le 11/01/2021 à 15:42, Léa Gris écrivait : Declare an integer associative array instead: echo "$BASH_VERSION" 5.0.17(1)-release declare -Ai aa x='y[$(date >&2)0]' aa[$x]=1 declare -p aa declare -Ai aa=(["y[\$(date >&2)0]"]="1" ) aa[$x]+=1 declare -p aa declare -Ai aa=(["y[\$(date >&2)0]"]="

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Greg Wooledge
On Mon, Jan 11, 2021 at 03:50:40PM +0100, Léa Gris wrote: > safe_arith_index=${x@Q} > declare -p safe_arith_index > > declare -- safe_arith_index="'y[\$(date >&2)0]'" > (( aa[$safe_arith_index]++ )) > declare -p aa > > declare -Ai aa=(["y[\$(date >&2)0]"]="3" ) OK, that one has potential, since it

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Oğuz
11 Ocak 2021 Pazartesi tarihinde Léa Gris yazdı: > > safe_arith_index=${x@Q} > This won't work if `x' contains a newline or a tab or something that makes bash resort to $'...' strings. See my first post. -- Oğuz

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Oğuz
11 Ocak 2021 Pazartesi tarihinde Chet Ramey yazdı: > > What semantics would you choose to change? > If you are reluctant to change semantics, how about a new parameter transformation to quote values using `sh_single_quote'? -- Oğuz

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Chet Ramey
On 1/11/21 11:33 AM, Oğuz wrote: 11 Ocak 2021 Pazartesi tarihinde Chet Ramey > yazdı: What semantics would you choose to change? If you are reluctant to change semantics, how about a new parameter transformation to quote values using `sh_single_quote'? Why

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Greg Wooledge
> > If you are reluctant to change semantics, how about a new parameter > > transformation to quote values using `sh_single_quote'? > > Why, when @Q already exists? It's often the case that the strings generated by bash are being fed to some other shell, where $'...' doesn't work. Granted, you c

Re: Associative array keys are not reusable in (( command

2021-01-11 Thread Oğuz
11 Ocak 2021 Pazartesi tarihinde Chet Ramey yazdı: > On 1/11/21 11:33 AM, Oğuz wrote: > >> >> >> 11 Ocak 2021 Pazartesi tarihinde Chet Ramey > chet.ra...@case.edu>> yazdı: >> >> What semantics would you choose to change? >> >> >> If you are reluctant to change semantics, how about a new param