Skip to content

Instantly share code, notes, and snippets.

@markeissler
Created July 30, 2022 00:07
Show Gist options
  • Save markeissler/3edd8ac48d9d54fae47663fe45295659 to your computer and use it in GitHub Desktop.
Save markeissler/3edd8ac48d9d54fae47663fe45295659 to your computer and use it in GitHub Desktop.
Strip comments and single quotes from dotenv files
#!/usr/bin/env bash
# SOME_VALUE='my_value'
# SOME_VALUE=my_value
# SOME_ARRAY='["one", "two", "three"]'
# SOME_ARRAY='["one","two","three"]'
# SOME_ARRAY=["one", "two", "three"]
input="${1}"
# Safeguard from overwriting a source file by making sure filename ends in `.raw`.
if [[ ! "${input}" =~ \.raw$ ]]; then
>&2 echo "source file does not have a '.raw' extension!" && exit 1
fi
if [[ ! -r "${input}" ]]; then
>&2 echo "source file does not look to a regular file!" && exit 1
fi
key="(\w+)"
pattern1="([^']+)"
gsed -rn -e "s/^$key=['\"]?$pattern1['\"]?$/\1=\2/p" "${input}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment