$_ CLI In Depth
Learning progress 0%
build functions arrays parameter-expansion

Build: An INI Config File Parser

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

The specification

Your parser reads one file, given as its first argument, and prints one line for each key. The output format is section.key=value. The rules are short. Lines that are blank, or whose first non-space character is a hash or a semicolon, are comments and are skipped. A line of the form [name] starts a section. A line with an equals sign is a key and value pair, and spaces around the key and the value are removed. Everything after the first equals sign belongs to the value, so values may contain spaces and more equals signs. Keys that appear before any section have no prefix.

TEXT13 lines
Input:
# sample
[server]
host = 127.0.0.1
port=8080
; comment
[db]
name = app data

Output:
server.host=127.0.0.1
server.port=8080
db.name=app data

Design decisions

Read the file with a while IFS= read -r line loop. The IFS= keeps leading whitespace, and -r keeps backslashes as they are. The loop must also handle a last line that has no trailing newline, which read reports as a failure even though it stored the text. Testing the line with || [[ -n $line ]] handles that case.

Trimming uses parameter expansion from the trimming unit, not sed, so the script starts no extra processes for each line. Removing leading whitespace means deleting everything up to the first non-space character, and removing trailing whitespace means deleting everything from the last non-space character to the end.

The reference solution

The solution below follows the specification. Read it line by line, and check how each piece of the spec maps to code. It is one possible design, and it is kept short on purpose.

BASH20 lines
#!/usr/bin/env bash
# Usage: ini.sh FILE
# Prints one section.key=value line for every key in FILE.
set -euo pipefail
file=${1:?usage: ini.sh FILE}
section=
while IFS= read -r line || [[ -n $line ]]; do
  line=${line#"${line%%[![:space:]]*}"}
  line=${line%"${line##*[![:space:]]}"}
  case $line in
    ''|'#'*|';'*) continue ;;
    \[*\]) section=${line:1:-1}; continue ;;
  esac
  [[ $line == *=* ]] || continue
  key=${line%%=*}
  value=${line#*=}
  key=${key%"${key##*[![:space:]]}"}
  value=${value#"${value%%[![:space:]]*}"}
  printf "%s.%s=%s\n" "$section" "$key" "$value"
done < "$file"

The first part trims the line in place using two expansions, one for the leading whitespace and one for the trailing. The case statement skips comments and blank lines, and it recognizes a section header by its brackets. The section name is the text between the first and last character, which the substring form ${line:1:-1} extracts. A line without an equals sign is skipped rather than printed as a broken key.

Why the output is flat

A flat format is easy to use with the tools from earlier modules. A caller can find one value with grep "^db.name=", or can cut a section with grep "^server\.". Choosing a line format rather than a nested structure keeps the parser small and makes its output easy to test with a diff.

Testing the parser

Test the parser against an input that has every feature in the spec, and against a file with edge cases such as a value containing an equals sign, a key with spaces around it, and a final line without a newline. The lab for this build asks you to write the parser and check it against two files. Use the output of the reference solution to compare your own output, and look for the first line that differs.

Extensions to try

Once the basic parser works, add a second argument that prints only one section, or make the parser exit with a message when a line has no section and no equals sign. Each extension is a few lines and reinforces one unit of this module, so treat the build as a place to combine what you have learned.

Terminal lab · 2 steps · real Linux in your browser

Build an INI parser

Write the parser, then test it on a file with comments, spaces, equals signs and no final newline.

01
Parse a simple INI file
The file app.ini is already in /workspace. Write ini.sh, a script that takes a file name and prints one section.key=value line for each setting. Use the reference solution in the build unit as your guide and write the script with a heredoc. Then run bash ini.sh app.ini and compare the output to the spec. The three lines should be server.host=127.0.0.1, server.port=8080 and db.name=app data.
Show hint for step 1
Read the file with while IFS= read -r line || [[ -n $line ]]. Skip blank lines and lines that start with a hash or semicolon after trimming, and print the section and key as one line.
02
Handle messy input
Run your ini.sh on messy.ini, which is in /workspace. It has an indented comment, spaces around keys and values, a value that contains an equals sign, a line without an equals sign, and a final line with no newline after it. The output must be web.name=site, web.path=/a=b and empty.last=final, in that order. Lines without an equals sign must be skipped, not printed.
Show hint for step 2
Trim each line first, so an indented comment is recognized. Split only on the first equals sign with ${line%%=*} and ${line#*=}, and keep the last line by testing read with || [[ -n $line ]].
Check yourself Question 1 of 4
Concept Check Reading lines without changing them recall

Which read form keeps leading whitespace and backslashes exactly as they are in the file?

Recap & Key Takeaways

The INI parser reads lines with IFS= and -r, handles a final line without a newline, and prints a flat section.key=value format.
Trimming and section extraction use parameter expansion, which keeps the script free of extra processes for each line.
Test the parser on edge cases such as values containing equals signs and comments with leading whitespace.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue