Sourceable Bash helper that reads INI-style config files. Bash has no built-in INI support; this script loads a file into per-section key/value arrays and optional section_key variables.
- Bash 4.3 or later (namerefs)
source src/ini-file-parser.sh
process_ini_file 'example.conf'
echo "$(get_value 'section1' 'value1')"
echo "${section1_value1}"A full walkthrough is in demos/parse-example.sh. Sample files:
| File | Description |
|---|---|
| simple example | Sections and key=value pairs. |
| complete example | Processing rules, warnings, and error conditions. |
- Empty lines are ignored.
- Lines starting with
#or;(after trim) are ignored. - Keys defined before the first section go in
default. - Section and key names keep only letters, digits, and underscores. Punctuation and blanks become
_. - Section and key names are case-sensitive by default.
- Leading and trailing whitespace is removed from keys and values.
- Inline
#or;comments are removed only when preceded by whitespace, so URL fragments and values such as#fffora;b;care kept. - Quotes around a value are kept.
'quoted value'is stored as'quoted value'. - Command-like text such as
$(ls)is stored as a string. The parser never executes it. - Duplicate keys in a section warn; the last value wins.
- After processing, each pair is also available as
section_key=value.
| Name | Purpose |
|---|---|
process_ini_file |
Load a named INI file. Returns 1 if the file cannot be read. |
get_value |
Return one value for a section and key. |
display_config |
Print the processed config in INI form. |
display_config_by_section |
Print one processed section. |
global_reset |
Drop all loaded sections, arrays, and scalars. |
get_version |
Print the library version string. |
Set these before process_ini_file. Values must be true or false.
| Name | Description | Default |
|---|---|---|
case_sensitive_sections |
Treat section names as case-sensitive. | true |
case_sensitive_keys |
Treat key names as case-sensitive. | true |
default_to_uppercase |
When case-insensitive, fold names to uppercase. | false |
show_config_warnings |
Print config warnings. | true |
show_config_errors |
Print config errors. | true |
Warnings are yellow and errors are red when that stream is a colour-capable
terminal. Set NO_COLOR to disable, or FORCE_COLOR=1 to enable in pipes.
show_config_warnings=false
case_sensitive_sections=false
source src/ini-file-parser.sh
process_ini_file 'example.conf'make init # first-time makefile-skills checkout
make check # shellcheck, bash -n, and tests/run_all.sh