I recently discovered that it is possible for Bash to set a variable to its default value when this variable is not set (as described in this post ).
Unfortunately, this does not work when the default is an array. As an example, consider
default_value=(0 1 2 3 4) my_variable=${my_variable:=("${default_value[@]}")} echo ${my_variable[0]} (0 a 2 3 4)
Does anyone know how to do this? Please note that changing :=
to :-
does not help.
Another problem is that any solution we get should also work when my_variable
already set in advance, so
my_variable=("a" "b" "c") default_value=(0 1 2 3 4) my_variable=${my_variable:=("${default_value[@]}")} echo ${my_variable[0]} "a" echo ${my_variable[1]} "b" echo ${my_variable[2]} "c"
source share