A bash script that automatically replaces 1Password references in .env files with their actual values, making it easy to manage secrets securely in your development workflow.
- 🔐 Securely fetch secrets from 1Password vault
- 🔄 Replace
op://references with actual values - 📝 Preserve comments and formatting in env files
- 🔍 Dry run mode to preview changes
- ✅ Verify mode to check all references are valid
- 💾 Automatic backup creation when overwriting files
- 🎨 Colored output for better readability
- 🛡️ Secure file permissions (600) for output files
-
1Password CLI: Install the 1Password command-line tool
# macOS brew install --cask 1password-cli # Linux (Ubuntu/Debian) curl -sS https://downloads.1password.com/linux/keys/1password.asc | \ sudo gpg --dearmor --output /usr/share/keyrings/1password-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/1password-archive-keyring.gpg] https://downloads.1password.com/linux/debian/$(dpkg --print-architecture) stable main" | \ sudo tee /etc/apt/sources.list.d/1password.list sudo apt update && sudo apt install 1password-cli
-
Sign in to 1Password:
# Sign in to your 1Password account eval $(op signin)
-
Download the script:
curl -O https://raw.githubusercontent.com/col/load_env_from_1pass/main/load_env_from_1pass.sh chmod +x load_env_from_1pass.sh
-
(Optional) Move to a directory in your PATH:
sudo mv load_env_from_1pass.sh /usr/local/bin/load_env_from_1pass
# Output to stdout
./load_env_from_1pass.sh .env.template
# Save to a new file
./load_env_from_1pass.sh .env.template .env
# Pipe to another command
./load_env_from_1pass.sh .env.template | grep DATABASEUsage: load_env_from_1pass.sh [OPTIONS] <input.env> [output.env]
Options:
-h, --help Show help message
-d, --dry-run Show what would be replaced without making changes
-v, --verify Verify that all references can be resolved
-
Replace references and save to file:
./load_env_from_1pass.sh .env.template .env
-
Dry run to see what will be replaced:
./load_env_from_1pass.sh --dry-run .env.template
-
Verify all references are valid:
./parse-1password-env.sh --verify .env.template
-
Use in a Docker build process:
./parse-1password-env.sh .env.template > .env docker-compose up
The script recognizes 1Password references in the following format:
op://vault/item/field
op://vault/item/section/field
# Simple reference
DATABASE_PASSWORD="op://Personal/PostgreSQL/password"
# With section
AWS_SECRET="op://DevOps/AWS/credentials/secret_key"
# Without quotes (also supported)
API_KEY=op://Work/API Keys/github_token
# Single quotes work too
SMTP_PASSWORD='op://Email/SMTP Server/password'- The script reads your
.envfile line by line - Identifies values that start with
op:// - Uses the 1Password CLI to fetch the actual value
- Replaces the reference with the real value
- Preserves quotes, comments, and formatting
- Outputs the result to stdout or a file
- File Permissions: Output files are automatically set to
600(read/write for owner only) - Backups: When overwriting existing files, a timestamped backup is created
- Memory: Secrets are only kept in memory during processing
- No Logging: The script doesn't log sensitive values
- Validation: Use
--verifymode to ensure all references are valid before deployment
# Keep a template in version control
git add .env.template
echo ".env" >> .gitignore
# Generate local .env file
./load_env_from_1pass.sh .env.template .env- Install the 1Password CLI following the prerequisites section
- Ensure
opis in your PATH:which op
- Run:
eval $(op signin) - For automated scripts, use:
op signin --raw > ~/.op-session
- Verify the reference path is correct:
op read "op://vault/item/field" - Check you have access to the vault/item
- Ensure the field name is spelled correctly
- Make the script executable:
chmod +x load_env_from_1pass.sh - Check write permissions for the output directory
- Version Control: Commit
.env.templatefiles, never.envfiles - Naming Convention: Use
.env.templateor.env.examplefor templates - Documentation: Document required 1Password items in your README
- Rotation: Regularly rotate secrets in 1Password
MIT License - Feel free to use and modify as needed.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues or questions:
- Check the troubleshooting section above
- Run with
set -xfor debug output - Open an issue on GitHub