ini_parser /

Filename Size Date modified Message
45 B
2.7 KB
5.6 KB

Functions for parsing INI-style file natively in bash.

The INI file format supports the following features:

Sections:   [section]
Properties: name=value
Comments:   ; comment 
            # comment

Blank lines and trailing writespace are ignored as is whitespace around 
the '=' - ie. 

   name  =  value

is equivalent to 


Whitespace and quotes within a value are preserved (though values don't 
in general need to be quoted and will not be subject to shell parameter 

Values can be continuted onto subsequent lines if these are prefixed with
whitespace - ie.


is equivalent to: 

   name = line1 line2

Properties are stored in the global _CONFIG array as ( k1 v1 k2 v2 ... ) with
keys in the format "<section>.<name>" (properties without an associated
section are stored as ".<name>". In most cases this is transparent as the
list/get commands can be used to query the data

The functionality is more or less equivalent to the Python ConfigParser 
module with the following exceptions

- Properties are allowed outside sections
- Multi-line properties are joined with ' ' rather than '\n' (due to shell
  quoting issues)


Given the following ini file (test.ini) -

  ; A test ini file
  global = a global value
  abc = def ; a comment
  ghi = jkl
  xyz = abc ; extends over two lines

Parse config file -

$ parseIniFile < test/t2.ini

$ listKeys

$ listAll
.global a global value def
section1.ghi jkl abc def

$ listSection section1 def
section1.ghi jkl

$ getProperty global
a global value

$ getProperty
abc def

$ getPropertyVar XYZ && echo OK

$ echo ">${XYZ}<"
>abc def<


parseIniFile < file

  Parse ini file (reads from stdin) and saves data to global _CONFIG var


  List keys present in config file in format "<section>.<property>"


  List keys and data in format "<section>.<property> <value>"

listSection <section>

  List keys and data for given section (sepcified as $1) in format
  "<property> <value>"

getProperty [name|]

  Print value for given property (sepcified as $1) 
  Properties without a section can be queried directly as
  "name" (rather than ".name")

  Returns 0 (true) if property found otherwise 1 (false)

getPropertyVar <variable> [name|]

  Save value for given property (sepcified as $2) 
  into shell variable (specified as $1)
  Properties without a section can be queried directly as
  "name" (rather than ".name")

  Returns 0 (true) if property found otherwise 1 (false)

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.