1

Consider the following example:

#!/bin/bash                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                 
declare -Ar foo_bar=(["hello"]="world")                                                                                                                                                                                                     

declare -p "foo_bar"

This outputs:

declare -Ar foo_bar=(["hello"]="world" )

But this example:

#!/bin/bash                                                                                                                                                                                                                                 

bar="bar"                                                                                                                                                                                                                                   

declare -Ar foo_"$bar"=(["hello"]="world")                                                                                                                                                                                                  

declare -p "foo_$bar"

Throws the error:

./test.sh: line 5: syntax error near unexpected token `('
./test.sh: line 5: `declare -Ar foo_"$bar"=(["hello"]="world")'

I'm not sure why the shell is thrown off by the variable expansion. This works perfectly fine:

declare -Ar foo_"$bar"'=(["hello"]="world")'

Or even this, which is what I'm currently using in my script to split a larger associative array over multiple lines:

declare -Ar foo_"$bar=""$(cat <<-EOF
    (
        ["hello"]="world"
    )
EOF
    )"

But, of course, I would prefer the cleaner:

declare -Ar foo_"$bar"=(
    ["hello"]="world"
)

Can someone give some insight on Bash internals to explain why the cleaner form doesn't work? I'm using Bash 5.12.15.


EDIT

This question is NOT a duplicate. It may sound similar to the linked "duplicates" (here and here), but is about something else (if you take the time to read it). My issue is with arrays combined with declare in particular; the example declare foo_"$bar"="hello world" works perfectly fine. Lastly, it asks about what Bash does internally to produce this syntax error.

2

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.