Environment files tend to start simple and slowly turn into a liability.
At first, you copy .env.template, change a few values, and move on. But as projects grow-multiple apps, multiple environments, multiple developers-that manual copying quickly becomes repetitive and error-prone.
A small Bash script can turn this process into something safer and more intentional: generating environment files from a template and filling in the missing values interactively.
The Idea: Treat .env as a Template
Instead of committing a ready-to-use .env.example, we start with a true template:
APP_NAME={{APP_NAME}}
DATABASE_URL={{DATABASE_URL}}
Every required value is marked explicitly using placeholders. If a variable exists in the template, it must be filled in.
This makes missing configuration impossible to ignore.
Step 1: Create a Bot-Specific Env File
The script starts by copying the template into a new, app-specific file:
ENV_TEMPLATE="./.env.template"
ENV_OUTPUT="./.env.${APP_NAME}.local"
echo "📝 Creating env file at $ENV_OUTPUT..."
cp "$ENV_TEMPLATE" "$ENV_OUTPUT"
Step 2: Detect All Placeholders Automatically
Instead of hardcoding variable names, the script scans the file for placeholders:
placeholders=$(grep -oE '\{\{[^}]+\}\}' "$ENV_OUTPUT" | sort | uniq)
Step 3: Prompt the User for Values
For each placeholder, the script:
- removes the curly braces
- asks the user for a value
- replaces it everywhere in the file
for placeholder in $placeholders; do
clean_name=${placeholder#<}
clean_name=${clean_name%>}
read -p "Enter value for $clean_name: " value
safe_value=$(printf '%s\n' "$value" | sed 's/[\/&]/\\&/g')
safe_placeholder=$(printf '%s\n' "$placeholder" | sed 's/[\/&]/\\&/g')
sed -i '' "s/$safe_placeholder/$safe_value/g" "$ENV_OUTPUT"
done
User input can contain special characters, so the script escapes both the placeholder and the value
Final Thoughts
This kind of script doesn't replace a secrets manager or a full provisioning system - but it fills an important gap between "manual copying" and "heavy infrastructure tooling."
Sometimes the best automation is small, local, and boring.
And that's exactly what you want for environment variables.
Top comments (0)