Default Values ββwith Associative Arrays
Bash arrays are not fixed-length arrays, so you cannot pre-initialize all elements. Indexed arrays are also not allowed, so you cannot use the default values ββas you think.
However, you can use associative arrays with an extension for missing values. For instance:
declare -A foo echo "${foo[bar]:-baz}"
This will return a baz for any missing key. Alternatively, instead of just returning the default value, you can actually set one of them to have no keys. For instance:
echo "${foo[bar]:=baz}"
This alternate call will not only return "baz", but will also store the value in an array for later use. Depending on your needs, any method should work for the case you are using.
source share