Array like data structure in bash (config file)? Array like data structure in bash (config file)? bash bash

Array like data structure in bash (config file)?


You can't nest data strutures in bash. At best, you can store the names of associative arrays in an array, and jump through indirection hoops to access them.

$ declare -A aa0=([name]=dataset1 [path]=/var/lib/bliTool/ds1 [type]=cvs )$ declare -A aa1=([name]=dataset2 [path]=/var/lib/bliTool/ds2 [type]=xml )$ declare -A aa2=([name]=dataset3 [path]=/home/igor/test/ds3 [type]=cvs )$ declare -a array=( aa0 aa1 aa2 )$ tmp=aa0[name]$ echo ${!tmp}dataset1  

For the second question, it's certainly possible to define a configuration file format with sections, but you'll need to write a parser that can process it. Other languages typically have a library available to parse rich configuration file formats.

As far as multiple paths per variable, stick with :. In theory, any delimiter could be used as part of a path name component, and so the delimiter needs to be quoted if it is part of a path. But since PATH uses : as its delimiter, there is historical awareness that : is not a great character to use in a path name, and that it needs to be quoted in PATH-like parameters.

path="/first/poor\:path\:name:/second/bad\:path\:name"

Then it will be up to your application to process the back-slashed :.


I came into a similar situation and my solution is to use different IFS within different level, it is somehow similar to chepner's idea. The code and sample can be found here https://github.com/pppoe/Nested-Array-Bash/