bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Associative array keys are not reusable in (( command


From: Oğuz
Subject: Associative array keys are not reusable in (( command
Date: Fri, 8 Jan 2021 13:20:08 +0300

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
"assoc[$'\n\'']++ ")

`(( assoc[\$key]++ ))' works as usual, but this is not documented as far as
I know, and not obvious to ordinary user at all. Considering that the
following two works, it doesn't make much sense that `(( assoc[$key]++ ))'
doesn't. It would be better if at least quoting the key by means of
parameter transformation or `printf %q' worked, because these are the first
workarounds that come to mind; it's not written anywhere that the
arithmetic evaluator can expand `$key' on its own.

    $ (( assoc[$'\n\'']++ ))
    $ declare -p assoc
    declare -A assoc=([$'\n\'']="43" )
    $
    $ (( assoc['
    '\']++ ))
    $ declare -p assoc
    declare -A assoc=([$'\n\'']="44" )

Oğuz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]