Skip to content

Instantly share code, notes, and snippets.

@petrabarus
Last active December 2, 2021 06:56
Show Gist options
  • Save petrabarus/794f291dbf4468c91e636cff56291d2c to your computer and use it in GitHub Desktop.
Save petrabarus/794f291dbf4468c91e636cff56291d2c to your computer and use it in GitHub Desktop.
1Password to Environment Variables
#!/usr/bin/env bash
# Load environment variables from 1password
# To execute this script, execute the command below.
# source 1passwordtoenv.sh 'Name of the item'
ITEM_NAME=$1
# Signin
eval $(op signin)
# Load environment variables
SECRETS=`op get item "${ITEM_NAME}"`
ENV_VARS=$(echo $SECRETS | jq -r '[.details.sections[] as $in | $in.fields[] as $h | $in | del(.fields) as $in2 | $h * $in2][] | select(.t != "") | "\(.t)=\(.v)"')
for ENV_VAR in $ENV_VARS; do
export $(echo $ENV_VAR)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment