From Bash manual for declare command:

When using -a or -A and the compound assignment syntax to create array variables, additional attributes do not take effect until subsequent assignments.

What does it mean?

I can't figure it out by

$ declare -ar arr=([1]=2, [2]=3)
$ declare -p arr
declare -ar arr='([1]="2," [2]="3")'



What it says is that attributes other than -a and -A won't have any effect during the first assignment.

Example with the uppercase attribute:

$ declare -a -u array=( hello )
$ printf '%s\n' "${array[@]}"
$ array+=( world )
$ printf '%s\n' "${array[@]}"
  • 1
    However, the documentation is incomplete in that it doesn't apply to -x or -r. Why it's that way is also not clear to me. That may be just to document a known limitation, but I can't imagine it being very hard to fix. – Stéphane Chazelas Jul 28 '17 at 23:01
  • @StéphaneChazelas You mean that -x has an immediate effect, right? I concur. OTOH, I'm not sure what you mean by "it doesn't apply to -r". -r has no effect during the first assignment, for all variable types. – xhienne Jul 28 '17 at 23:10
  • I mean it doesn't need another assignment to make the variable read-only. It's already read-only by the time typeset returns (as in the attribute takes effect straight away). That's the case mentioned by the OP. – Stéphane Chazelas Jul 28 '17 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.