Bash INI Parser

A robust shell script library for parsing and manipulating INI configuration files in Bash.

Version: 0.0.1
License: BSD
Author: Leandro Ferreira
GitHub: lsferreira42/bash-ini-parser

Try It Now

Experiment with the bash-ini-parser library directly in your browser! The terminal below has access to all example files.

How to use:

To test the library, run these commands:

  1. ls - List available files
  2. cat lib_ini.sh - View the library code
  3. source lib_ini.sh - Load the library in the current session
  4. cat examples/simple.ini - View an example INI file
  5. Now you can use commands like ini_read simple.ini app name
bash-ini-parser demo

Features

Installation

Simply include the lib_ini.sh script in your project and source it in your shell scripts:

source /path/to/lib_ini.sh

Download Options

Clone the repository:

git clone https://github.com/lsferreira42/bash-ini-parser.git

Or download the script directly:

curl -o lib_ini.sh https://raw.githubusercontent.com/lsferreira42/bash-ini-parser/main/lib_ini.sh

Basic Usage

Here's a simple example of how to use the library:

#!/bin/bash
source ./lib_ini.sh

# Create a new INI file with sections and keys
CONFIG_FILE="config.ini"
ini_add_section "$CONFIG_FILE" "app"
ini_write "$CONFIG_FILE" "app" "name" "My Application"
ini_write "$CONFIG_FILE" "app" "version" "1.0.0"

# Read values
app_name=$(ini_read "$CONFIG_FILE" "app" "name")
echo "App name: $app_name"

# List sections and keys
echo "Available sections:"
ini_list_sections "$CONFIG_FILE" | while read section; do
    echo "- $section"
    echo "  Keys:"
    ini_list_keys "$CONFIG_FILE" "$section" | while read key; do
        value=$(ini_read "$CONFIG_FILE" "$section" "$key")
        echo "  - $key = $value"
    done
done

# Remove a key
ini_remove_key "$CONFIG_FILE" "app" "name"

# Remove a section
ini_remove_section "$CONFIG_FILE" "app"

Advanced Features

Array Support

# Write array values
ini_write_array "$CONFIG_FILE" "app" "supported_formats" "jpg" "png" "gif"

# Read array values
formats=$(ini_read_array "$CONFIG_FILE" "app" "supported_formats")
for format in $formats; do
    echo "Format: $format"
done

Default Values

# Get a value or use a default if not found
timeout=$(ini_get_or_default "$CONFIG_FILE" "app" "timeout" "30")

Environment Variables Export

# Export all INI values to environment variables with a prefix
ini_to_env "$CONFIG_FILE" "CFG"
echo "App name from env: $CFG_app_name"

# Export only one section
ini_to_env "$CONFIG_FILE" "CFG" "database"

File Import

# Import all values from one INI file to another
ini_import "defaults.ini" "config.ini"

# Import only specific sections
ini_import "defaults.ini" "config.ini" "section1" "section2"

Key Existence Check

if ini_key_exists "config.ini" "app" "version"; then
    echo "The key exists"
fi

Configuration Options

The library's behavior can be customized by setting these variables either directly in your script after sourcing the library or as environment variables before sourcing the library:

Method 1: Set in your script after sourcing

source ./lib_ini.sh
INI_DEBUG=1

Method 2: Set as environment variables before sourcing

export INI_DEBUG=1
source ./lib_ini.sh

Available configuration options:

# Enable debug mode to see detailed operations
INI_DEBUG=1

# Enable strict validation of section and key names
INI_STRICT=1

# Allow empty values
INI_ALLOW_EMPTY_VALUES=1

# Allow spaces in section and key names
INI_ALLOW_SPACES_IN_NAMES=1

Examples

Check the examples directory for complete usage examples: