A shell script to manage environment variables using 1Password.
- Reads variable names from
.env.example
- Only manages variables that exist in 1Password
- Uses
source
to properly modify shell environment - Never exposes sensitive values in output
- Supports configuration via environment variables or command line arguments
- 1Password CLI (op) version 2.30 or higher
- A
.env.example
file in your project listing the environment variables - Bash shell
Using Homebrew:
brew tap serraict/op-env https://github.com/serraict/op-env
brew install op-env
To upgrade to the latest version:
brew update
brew upgrade op-env
The script must be sourced (not executed) to modify environment variables. You can configure it using environment variables or command line arguments:
# Set configuration
export OP_VAULT="Your Vault"
export OP_ITEM="Your Item"
# Load variables from 1Password
source op-env load
# Unset variables that exist in 1Password
source op-env unset
# Load variables from 1Password
source op-env load "Your Vault" "Your Item"
# Unset variables that exist in 1Password
source op-env unset "Your Vault" "Your Item"
Given a .env.example
file:
API_KEY=your_api_key
CLIENT_ID=your_client_id
DEBUG=true
BASE_URL=http://localhost:3000
And a 1Password item containing API_KEY
and CLIENT_ID
, running:
# Using environment variables
export OP_VAULT="My Vault"
export OP_ITEM="Project Secrets"
source op-env load
# Or using command line arguments
source op-env load "My Vault" "Project Secrets"
Will:
- Set
API_KEY
andCLIENT_ID
from 1Password - Ignore
DEBUG
andBASE_URL
(not in 1Password) - Show which variables were set/ignored
Run the test script to verify functionality:
./test/test.sh "Your Vault" "Your Item"