Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Configuration file reading in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Configuration file reading
Start script
↓
Open config file
↓
Read line
↓
Is line empty or comment?
Yes→Skip line
No↓
Parse key=value
↓
Store in variable
↓
More lines?
Yes→Read line
No↓
Use variables
↓
End script
The script reads each line of the config file, skips empty or comment lines, extracts key-value pairs, stores them, and then uses these variables.
Execution Sample
Bash Scripting
#!/bin/bash
while IFS='=' read -r key value; do
  [[ $key =~ ^#.*$ || -z $key ]] && continue
  declare "$key=$value"
done < config.cfg

echo "User: $user"
echo "Port: $port"
Reads a config file line by line, skips comments and empty lines, assigns variables, then prints them.
Execution Table
StepLine ReadCondition (Comment/Empty?)ActionVariables SetOutput
1user=aliceNoSet user=aliceuser=alice
2# This is a commentYesSkip lineuser=alice
3port=8080NoSet port=8080user=alice, port=8080
4YesSkip lineuser=alice, port=8080
5mode=activeNoSet mode=activeuser=alice, port=8080, mode=active
6EOFN/AEnd of fileuser=alice, port=8080, mode=activeUser: alice Port: 8080
💡 Reached end of config file, all variables set, script prints values.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
userunsetalicealicealicealicealicealice
portunsetunsetunset8080808080808080
modeunsetunsetunsetunsetunsetactiveactive
Key Moments - 3 Insights
Why does the script skip lines starting with # or empty lines?
Lines starting with # are comments and empty lines have no data, so skipping them avoids errors and only processes valid key=value pairs, as shown in steps 2 and 4 of the execution_table.
How does the script assign variables from the config file?
It reads each line, splits at '=', then uses declare to create variables dynamically, shown in steps 1, 3, and 5 where variables user, port, and mode are set.
What happens if the config file has spaces around '='?
The script may include spaces in keys or values causing incorrect variable names or values; trimming spaces before assignment is needed but not shown here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'port' after step 3?
Aunset
B8080
Calice
Dactive
💡 Hint
Check the 'Variables Set' column at step 3 in the execution_table.
At which step does the script skip a line because it is a comment?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Condition' and 'Action' columns in the execution_table for skipped lines.
If the config file had an empty line at step 3 instead of 'port=8080', what would happen to the 'port' variable?
AIt would remain unset
BIt would be set to '8080'
CIt would be set to empty string
DScript would error out
💡 Hint
Refer to how empty lines are handled in the execution_table and variable_tracker.
Concept Snapshot
Read config file line by line with 'while read'.
Skip empty lines and comments starting with '#'.
Split lines by '=' to get key and value.
Use 'declare' to set variables dynamically.
Use variables later in the script as needed.
Full Transcript
This script reads a configuration file line by line. It skips lines that are empty or start with a comment symbol (#). For each valid line, it splits the line into a key and a value at the '=' sign. Then it creates a variable named after the key and assigns it the value. After reading all lines, the script uses these variables, for example, printing the user and port values. This process helps scripts get settings from files easily.

Practice

(1/5)
1. What is the main purpose of reading a configuration file in a bash script?
easy
A. To load variable values that the script can use
B. To execute commands from the internet
C. To delete files automatically
D. To compile the script into a binary

Solution

  1. Step 1: Understand configuration files

    Configuration files usually contain variable assignments that scripts read to get settings.
  2. Step 2: Purpose of reading config files

    Reading these files allows scripts to use flexible values without hardcoding them.
  3. Final Answer:

    To load variable values that the script can use -> Option A
  4. Quick Check:

    Config files provide variables = B [OK]
Hint: Config files store variables for scripts to use [OK]
Common Mistakes:
  • Thinking config files run commands directly
  • Confusing config files with executable scripts
  • Assuming config files delete or compile code
2. Which of the following is the correct syntax to read a config file named settings.conf in a bash script?
easy
A. source settings.conf
B. read settings.conf
C. import settings.conf
D. execute settings.conf

Solution

  1. Step 1: Identify the command to read config files

    In bash, source or . is used to read and execute commands from a file in the current shell.
  2. Step 2: Check options

    read reads input, import is not a bash command, and execute is invalid.
  3. Final Answer:

    source settings.conf -> Option A
  4. Quick Check:

    Use source or . to read config [OK]
Hint: Use 'source' or '.' to load config files [OK]
Common Mistakes:
  • Using 'read' command instead of 'source'
  • Trying to 'import' like in other languages
  • Assuming 'execute' runs the config file
3. Given the config file app.conf with content:
USERNAME=guest
PORT=8080

What will the output of this script be?
source app.conf
echo "User: $USERNAME, Port: $PORT"
medium
A. User: $USERNAME, Port: $PORT
B. User: , Port:
C. Syntax error
D. User: guest, Port: 8080

Solution

  1. Step 1: Source the config file

    The source app.conf command loads variables USERNAME and PORT into the script.
  2. Step 2: Print the variables

    The echo command prints the values of USERNAME and PORT, which are 'guest' and '8080'.
  3. Final Answer:

    User: guest, Port: 8080 -> Option D
  4. Quick Check:

    Variables loaded and printed = C [OK]
Hint: Sourcing sets variables, echo prints their values [OK]
Common Mistakes:
  • Not sourcing the file before using variables
  • Expecting variable names printed literally
  • Confusing syntax causing errors
4. What is wrong with this script snippet that tries to read config.cfg and print a variable?
source config.cfg
echo "Value: $VALUE"

Assuming config.cfg contains VALUE=42, but the output is empty.
medium
A. The script forgot to use 'export' before sourcing
B. The config file has spaces around '=' causing no assignment
C. The variable name is case-sensitive and mismatched
D. The source command is misspelled

Solution

  1. Step 1: Check config file syntax

    If the config file has spaces like VALUE = 42, bash treats it as a command, not assignment.
  2. Step 2: Effect on sourcing

    Because of wrong syntax, VALUE is not set, so echo prints empty.
  3. Final Answer:

    The config file has spaces around '=' causing no assignment -> Option B
  4. Quick Check:

    No spaces in var assignment = A [OK]
Hint: No spaces around '=' in config files [OK]
Common Mistakes:
  • Adding spaces around '=' in variable assignments
  • Thinking 'export' is needed before sourcing
  • Ignoring case sensitivity of variable names
5. You want to safely read a config file settings.conf that might contain unsafe commands. Which approach is best to avoid running unwanted code while still getting variable values?
hard
A. Run the config file as a separate script
B. Use source settings.conf directly without checks
C. Manually parse the file line-by-line to extract variable assignments
D. Delete the config file before sourcing

Solution

  1. Step 1: Understand risks of sourcing

    Using source runs all commands in the file, which can be unsafe if file is untrusted.
  2. Step 2: Safe alternative

    Manually reading lines and extracting only variable assignments avoids executing harmful commands.
  3. Final Answer:

    Manually parse the file line-by-line to extract variable assignments -> Option C
  4. Quick Check:

    Parse manually to avoid unsafe code = A [OK]
Hint: Parse config lines manually to avoid running code [OK]
Common Mistakes:
  • Sourcing untrusted config files directly
  • Running config as a script without validation
  • Deleting config file instead of securing it
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard