Skip to content

Instantly share code, notes, and snippets.

@nightsparc
Created May 25, 2022 12:33
Show Gist options
  • Save nightsparc/160981a7f2c7e2290b5d58f1416c0592 to your computer and use it in GitHub Desktop.
Save nightsparc/160981a7f2c7e2290b5d58f1416c0592 to your computer and use it in GitHub Desktop.
Read key value pairs from a file
# @brief Helper to read key value pairs
# @details
# Read kv in the form <key>=<value>
# The script cleans whitespaces first and will remove any newlines
# @see https://stackoverflow.com/a/27918723/1267320
read_kvp()
{
file="$1"
while IFS="=" read -r key value; do
# clean whitespaces
keyClean=$(echo $key)
valClean=$(echo $value)
# echo "$keyClean = $valClean"
case "$keyClean" in
'#'*) ;;
*)
eval "$keyClean=\"${valClean//[$'\t\r\n']}\""
esac
done < "$file"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment