Skip to content

Instantly share code, notes, and snippets.

@briceburg
Created April 2, 2025 17:35
Show Gist options
  • Save briceburg/979a70af0b6644dba68fc64a9a783b6c to your computer and use it in GitHub Desktop.
Save briceburg/979a70af0b6644dba68fc64a9a783b6c to your computer and use it in GitHub Desktop.
dump the current environment into .env file format (multiline values are quoted) while ignorning common variables.
#!/usr/bin/env bash
# dumps the current environment into .env file format (multiline values are quoted) while ignorning common variables.
# additional ignore list can be provided as arguments.
# common variables to ignore
declare -a COMMON_IGNORE_LIST=(
"_"
"COLORTERM"
"GEM_CACHE"
"GEM_HOME"
"GEM_PATH"
"GEM_ROOT"
"GEM_SPEC_CACHE"
"HOME"
"HOSTNAME"
"JAVA_HOME"
"JAVA_OPTS"
"LANG"
"LC_CTYPE"
"LC_TYPE"
"LOGNAME"
"MAIL"
"OLDPWD"
"PATH"
"PS1"
"PWD"
"RUBY_ROOT"
"SHELL"
"SHLVL"
"SSH_AGENT_PID"
"SSH_AUTH_SOCK"
"SSH_CONNECTION"
"SSH_TTY"
"TERM_PROGRAM_VERSION"
"TERM_PROGRAM"
"TERM_SESSION_ID"
"TERM"
"TMPDIR"
"USER"
)
# additional ignore list
declare -a ADDITIONAL_IGNORE_LIST=("$@")
# combine ignore lists
declare -a IGNORE_LIST=("${COMMON_IGNORE_LIST[@]}" "${ADDITIONAL_IGNORE_LIST[@]}")
# dump environment
for var in $(compgen -e); do
if [[ ! " ${IGNORE_LIST[*]} " =~ " ${var} " ]]; then
echo "${var}=\"${!var//\"/\\\"}\""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment