2

Have associative array

OPTIONS[a]="a-value"

have another array id and need set a value from OPTIONS only when some value is NOT set, so something like

id[KEY1]=${id[KEY1]:-OPTIONS[a]}

but this not works.

How to use the bash's :- "variable substitution" with associative arrays?

1 Answer 1

3

You were pretty close. This works for me:

$ OPTIONS[a]="a-value"
$ id[KEY1]="b"
$ id[KEY1]=${id[KEY1]:-${OPTIONS[a]}}
$ echo ${id[KEY1]}
b
$ unset id
$ id[KEY1]=${id[KEY1]:-${OPTIONS[a]}}
$ echo ${id[KEY1]}
a-value
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Why is it now obvious? :) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.