Skip to content

Instantly share code, notes, and snippets.

@gwerbin
Created July 19, 2023 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwerbin/8b3625b91a448e1b83f6bbc68a35523a to your computer and use it in GitHub Desktop.
Save gwerbin/8b3625b91a448e1b83f6bbc68a35523a to your computer and use it in GitHub Desktop.
Quoting "dotenv" files.
dotenv list --format=shell
#!/usr/bin/env -S sed -Ef
# Delete blank and commented-out lines.
/^[[:space:]]*(#|$)/ d
# Replace single quotes with the "unquote-escape-quote" trick:
# 1'2'3 → '1'\''2'\''3'
# i.e. the concatenation of:
# '1' \' '2' \' '3'
s/'/'\\''/g
# Wrap the right-hand side of each key=value line with single quotes.
s/=(.*)/='\1'/g
#!/usr/bin/env zsh
zmodload zsh/regex
setopt err_exit no_unset warn_create_global warn_nested_var re_match_pcre
function _quote_dotenv {
local MATCH MBEGIN MEND # created by =~
local -a match mbegin mend # created by =~
local line
while read -r line; do
if [[ ! "$line" =~ '^[[:space:]]*(#|$)' ]]; then
print -r -- "${line%%=*}=${(q)${line#*=}}"
fi
done
}
if (( $# > 0 )); then
for arg in "${@}"; do
_quote_dotenv < "$arg"
done
else
_quote_dotenv
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment