Skip to content

Instantly share code, notes, and snippets.

@melbahja
Created January 14, 2020 21:43
Show Gist options
  • Save melbahja/33fac6f3f823632e880401f5f7451cfb to your computer and use it in GitHub Desktop.
Save melbahja/33fac6f3f823632e880401f5f7451cfb to your computer and use it in GitHub Desktop.
Parse and loop through key value pairs in bash
# this is a comment
MYVAR=My value
OKOK=asas
YES=asja
#!/usr/bin/bash
# Copyright 2020 - MIT, Mohamed Elbahja (http://git.io/mohamed).
cat file.conf | while read line || [[ -n $line ]];
do
[[ ${line//[[:space:]]/} =~ ^#.* || -z "$line" ]] && continue
echo $line | tr "=" "\n" | while read -r key; do
read -r value
echo "key: ${key} and value: ${value}";
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment