Many simple config files use name=value lines. A script can read each line and keep the value for the key it needs.

Program

Play the script to scan fixed config text for a selected key.

selected_key
parse_key_values.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

config=$'host=local\nport=8080\ndebug=false'
selected_key="port"
selected_value=""
while IFS="=" read -r key value; do
    if [[ "$key" == "$selected_key" ]]; then
        selected_value=$value
    fi
done <<< "$config"
echo "$selected_key=$selected_value"
#!/usr/bin/env bash

config=$'host=local\nport=8080\ndebug=false'
selected_key="host"
selected_value=""
while IFS="=" read -r key value; do
    if [[ "$key" == "$selected_key" ]]; then
        selected_value=$value
    fi
done <<< "$config"
echo "$selected_key=$selected_value"
#!/usr/bin/env bash

config=$'host=local\nport=8080\ndebug=false'
selected_key="debug"
selected_value=""
while IFS="=" read -r key value; do
    if [[ "$key" == "$selected_key" ]]; then
        selected_value=$value
    fi
done <<< "$config"
echo "$selected_key=$selected_value"
  1. config ← host, port, debug

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="port"
    values this stephost, port, debugconfig
  2. selected_key ← port

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="port"5selected_value=""
    values this stepportselected_key
  3. selected_value ← (empty)

    4selected_key="port"5selected_value=""6while IFS="=" read -r key value; do
    values this step(empty)selected_value
  4. key ← host, value ← local

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stephostkeylocalvalue
  5. key ← port, value ← 8080

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepportkey8080value
  6. selected_value ← 8080

    7if [[ "$key" == "$selected_key" ]]; then8    selected_value=$value9fi
    values this step8080selected_valueportkey8080value
  7. key ← debug, value ← false

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepdebugkeyfalsevalue
  8. echo "$selected_key=$selected_value"

    10done <<< "$config"11echo "$selected_key=$selected_value"
    outputport=8080
    values this stepportselected_key8080selected_value
  1. config ← host, port, debug

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="host"
    values this stephost, port, debugconfig
  2. selected_key ← host

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="host"5selected_value=""
    values this stephostselected_key
  3. selected_value ← (empty)

    4selected_key="host"5selected_value=""6while IFS="=" read -r key value; do
    values this step(empty)selected_value
  4. key ← host, value ← local

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stephostkeylocalvalue
  5. selected_value ← local

    7if [[ "$key" == "$selected_key" ]]; then8    selected_value=$value9fi
    values this steplocalselected_valuehostkeylocalvalue
  6. key ← port, value ← 8080

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepportkey8080value
  7. key ← debug, value ← false

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepdebugkeyfalsevalue
  8. echo "$selected_key=$selected_value"

    10done <<< "$config"11echo "$selected_key=$selected_value"
    outputhost=local
    values this stephostselected_keylocalselected_value
  1. config ← host, port, debug

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="debug"
    values this stephost, port, debugconfig
  2. selected_key ← debug

    3config=$'host=local\nport=8080\ndebug=false'4selected_key="debug"5selected_value=""
    values this stepdebugselected_key
  3. selected_value ← (empty)

    4selected_key="debug"5selected_value=""6while IFS="=" read -r key value; do
    values this step(empty)selected_value
  4. key ← host, value ← local

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stephostkeylocalvalue
  5. key ← port, value ← 8080

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepportkey8080value
  6. key ← debug, value ← false

    5selected_value=""6while IFS="=" read -r key value; do7    if [[ "$key" == "$selected_key" ]]; then
    values this stepdebugkeyfalsevalue
  7. selected_value ← false

    7if [[ "$key" == "$selected_key" ]]; then8    selected_value=$value9fi
    values this stepfalseselected_valuedebugkeyfalsevalue
  8. echo "$selected_key=$selected_value"

    10done <<< "$config"11echo "$selected_key=$selected_value"
    outputdebug=false
    values this stepdebugselected_keyfalseselected_value

Follow the Key

  1. The config text has host=local, port=8080, and debug=false.
  2. selected_key starts as port.
  3. The loop reads one key=value line at a time.
  4. Only the port line matches, so selected_value becomes 8080.
  5. The script prints port=8080. | selected_key | matching line | selected_value | output | | --- | --- | --- | --- | | host | host=local | local | host=local | | port | port=8080 | 8080 | port=8080 | | debug | debug=false | false | debug=false |
IFS `IFS="="` makes `read` split each line around the equals sign.
read loop `while read ...; do` processes one config line per iteration.
selected key The `if` keeps only the value whose key matches the selected setting.

Exercise: parse_key_values.sh

Reproduce port=8080, then use selected_key host and debug to predict host=local and debug=false.