0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Read Properties File Easily

Use a Bash script with a while IFS='=' read -r key value loop to read each line of the properties file and assign variables dynamically.
📋

Examples

Inputapp.name=ChatApp app.version=1.0 app.debug=true
Outputapp.name=ChatApp app.version=1.0 app.debug=true
Inputusername=alice password=secret host=localhost
Outputusername=alice password=secret host=localhost
Input# Comment line key1=value1 key2=value2
Outputkey1=value1 key2=value2
🧠

How to Think About It

To read a properties file, treat each line as a key=value pair. Skip empty lines and comments. Use a loop to read each line, split it at the '=' sign, and assign the key and value to variables for use in the script.
📐

Algorithm

1
Open the properties file for reading.
2
For each line, ignore if it is empty or starts with a comment character (#).
3
Split the line into key and value parts at the first '=' character.
4
Trim whitespace from key and value.
5
Assign the value to a variable named after the key or store it for later use.
6
Repeat until all lines are processed.
💻

Code

bash
#!/bin/bash

properties_file="config.properties"

while IFS='=' read -r key value; do
  # Skip empty lines and comments
  [[ -z "$key" || "$key" =~ ^# ]] && continue
  # Trim whitespace
  key=$(echo "$key" | xargs)
  value=$(echo "$value" | xargs)
  echo "$key=$value"
done < "$properties_file"
Output
app.name=ChatApp app.version=1.0 app.debug=true
🔍

Dry Run

Let's trace reading the line 'app.name=ChatApp' through the code

1

Read line

Line read: 'app.name=ChatApp'

2

Split line

key='app.name', value='ChatApp'

3

Trim whitespace

key='app.name', value='ChatApp'

4

Print key=value

Output: 'app.name=ChatApp'

StepKeyValueOutput
1app.nameChatAppapp.name=ChatApp
💡

Why This Works

Step 1: Reading lines

The while IFS='=' read -r key value reads each line splitting at the first '=' into key and value.

Step 2: Skipping comments and empty lines

Lines starting with # or empty lines are ignored using a conditional check.

Step 3: Trimming whitespace

Using xargs trims spaces around keys and values to clean the data.

Step 4: Using the values

The script prints or can assign the values to variables for later use.

🔄

Alternative Approaches

Using source command
bash
#!/bin/bash
source config.properties
echo "$app_name $app_version $app_debug"
This works if the properties file is valid Bash syntax without spaces around '=' and no comments; less safe but simpler.
Using awk to parse
bash
awk -F '=' '/^[^#]/ && NF==2 {gsub(/^[ \t]+|[ \t]+$/, "", $1); gsub(/^[ \t]+|[ \t]+$/, "", $2); print $1"="$2}' config.properties
Awk can parse and trim lines efficiently but requires understanding awk syntax.

Complexity: O(n) time, O(1) space

Time Complexity

The script reads each line once, so time grows linearly with the number of lines.

Space Complexity

Uses constant extra space for variables; no large data structures are stored.

Which Approach is Fastest?

Using source is fastest but less safe; the loop method is safer and flexible.

ApproachTimeSpaceBest For
while read loopO(n)O(1)Safe parsing with trimming and skipping
source commandO(n)O(1)Simple files without spaces or comments
awk parsingO(n)O(1)Efficient parsing with trimming, requires awk knowledge
💡
Always skip empty lines and comments when reading properties files to avoid errors.
⚠️
Beginners often forget to handle spaces around '=' causing incorrect key or value extraction.