Bash Array value in default value assignment

I am trying to assign an array of three values ​​to a variable if it is not already assigned with a line

: ${SEAFILE_MYSQL_DB_NAMES:=(ccnet-db seafile-db seahub-db)}

Unfortunately, the echo ${SEAFILE_MYSQL_DB_NAMES[@]} results in (ccnet-db seafile-db seahub-db) , and ${SEAFILE_MYSQL_DB_NAMES[2]} does not print anything. The value seems to be interpreted as a string, not as an array. Is there a way to make my script assign an array this way?

This problem occurs in Debian Jessie with bash 4.3.30 (if that matters in the docker container). Interestingly, the same code runs on Ubuntu 16.04 with bash version 4.3.42, where it is treated as an array, as I would expect.

+5
source share
1 answer

How to do it in several stages? First declare a backup array, then check to see if SEAFILE_MYSQL_DB_NAMES is set and assign if necessary.

 DBS=(ccnet-db seafile-db seahub-db) [[ -v SEAFILE_MYSQL_DB_NAMES ]] || read -ra SEAFILE_MYSQL_DB_NAMES <<< ${DBS[@]} 

Based on this answer .

+2
source

All Articles