Skip to content

Instantly share code, notes, and snippets.

@jercle
Created June 17, 2022 05:59
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 jercle/52de04288126f1c0721091cf7b13459f to your computer and use it in GitHub Desktop.
Save jercle/52de04288126f1c0721091cf7b13459f to your computer and use it in GitHub Desktop.
Environment Variable Setter - Adding to .zshrc
fex() {
if [ -z "$1" ]; then
if [ ! -f ~/.env ]; then
echo "~/.env not found!"
else
echo "Export the following from ~/.env:"
grep -v "^#" ~/.env | grep -v -e '^[[:space:]]*$'
export $(grep -v "^#" ~/.env | grep -v -e '^[[:space:]]*$' | xargs)
fi
else
if [[ "$1" == "mod" ]]; then
code ~/.env
elif [[ "$1" == "unset" ]]; then
if [ ! -f ~/.env ]; then
echo "./.env not found!"
else
echo "Unset the following from ~/.env:"
grep -v "^#" ~/.env | grep -v -e '^[[:space:]]*$' | sed -E "s/(.*)=.*/\1/"
unset $(grep -v "^#" ~/.env | grep -v -e '^[[:space:]]*$' | sed -E "s/(.*)=.*/\1/" | xargs)
fi
else
if [ -z "$2" ]; then
if [ ! -f "$1"/.env ]; then
echo "$1/.env not found!"
else
echo "Set the following from $1/.env:"
grep -v "^#" "$1"/.env | grep -v -e '^[[:space:]]*$'
export $(grep -v "^#" "$1"/.env | grep -v -e '^[[:space:]]*$' | xargs)
fi
else
if [[ "$2" == "unset" ]]; then
if [ ! -f "$1"/.env ]; then
echo "$1/.env not found!"
else
echo "Unset the following from $1/.env:"
grep -v "^#" "$1"/.env | grep -v -e '^[[:space:]]*$' | sed -E "s/(.*)=.*/\1/"
unset $(grep -v "^#" "$1"/.env | grep -v -e '^[[:space:]]*$' | sed -E "s/(.*)=.*/\1/" | xargs)
fi
elif [[ "$2" == "mod" ]]; then
code "$1"/.env
fi
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment