Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active April 12, 2025 23:52
Show Gist options
  • Save iTrooz/4caeaf2f26c5d4a1ee06f33917cd6fa7 to your computer and use it in GitHub Desktop.
Save iTrooz/4caeaf2f26c5d4a1ee06f33917cd6fa7 to your computer and use it in GitHub Desktop.
Execute a command with environment set from .env
#!/bin/sh
# Source: https://gist.github.com/iTrooz/4caeaf2f26c5d4a1ee06f33917cd6fa7
# Syntax: loadenv <command> [args]
if [ -f .env ]
then
while IFS= read -r line || [ -n "$line" ]; do
var="${line%%#*}"
[[ -n "$var" ]] && eval "export $var" # Eval is needed for quoted values
done < .env
else
>&2 echo "Warning: no .env file found"
fi
$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment