Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Configuration file reading in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Config File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Reading a simple config file with key-value pairs
Given a config file settings.conf with the content:
username=admin
password=secret
port=8080

What is the output of this script?
while IFS='=' read -r key value; do
  echo "$key -> $value"
done < settings.conf
Bash Scripting
while IFS='=' read -r key value; do
  echo "$key -> $value"
done < settings.conf
A
username -&gt; admin
password -&gt; secret
port -&gt; 8080
B
username
admin
password
secret
port
8080
C
username=admin
password=secret
port=8080
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Think about how the read command splits lines using the IFS variable.
💻 Command Output
intermediate
2:00remaining
Ignoring comments and blank lines in config file
Consider a config file app.conf with:
# This is a comment
host=localhost

# Another comment
port=3306

What does this script output?
grep -v '^#' app.conf | grep -v '^$' | while IFS='=' read -r key value; do
  echo "$key=$value"
done
Bash Scripting
grep -v '^#' app.conf | grep -v '^$' | while IFS='=' read -r key value; do
  echo "$key=$value"
done
A
host=localhost
port=3306
B
# This is a comment
host=localhost

# Another comment
port=3306
C
host
localhost
port
3306
DError: command not found
Attempts:
2 left
💡 Hint
grep -v '^#' removes comment lines, grep -v '^$' removes blank lines.
📝 Syntax
advanced
2:00remaining
Parsing config with spaces around equals sign
Which script correctly reads a config file where lines may have spaces around '=' like:
user = admin
password = secret

and prints key and value?
Awhile IFS='=' read key value; do echo "$key -> $value"; done < config.txt
Bwhile IFS='=' read -r key value; do echo "$key -> $value"; done < config.txt
Cwhile IFS=' = ' read -r key value; do echo "$key -> $value"; done < config.txt
Dwhile read -r line; do key=${line%%=*}; value=${line#*=}; echo "$key -> $value"; done < config.txt
Attempts:
2 left
💡 Hint
Think about how to trim spaces around keys and values.
🔧 Debug
advanced
2:00remaining
Why does this config reading script fail?
This script tries to read config.cfg and print keys and values:
while IFS='=' read -r key value
 do
  echo "$key: $value"
done < config.cfg

But it prints nothing. What is the problem?
Bash Scripting
while IFS='=' read -r key value
 do
  echo "$key: $value"
done < config.cfg
AFile config.cfg is empty
BMissing semicolon or newline after read command causes syntax error
CIFS should be set to ':=' instead of '='
Dread command needs -a option to read arrays
Attempts:
2 left
💡 Hint
Check the syntax of the while loop and read command carefully.
🚀 Application
expert
3:00remaining
Extracting config values into variables
Given a config file settings.ini with:
host=127.0.0.1
port=8080
user=guest

Which script correctly reads these values and sets bash variables host, port, and user accordingly?
Awhile IFS='=' read -r key value; do declare "$key=$value"; done < settings.ini
Bsource settings.ini
Cwhile IFS='=' read -r key value; do export $key=$value; done < settings.ini
Deval $(sed 's/^/export /' settings.ini)
Attempts:
2 left
💡 Hint
Think about how to safely assign variables from a file in bash.

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