bw-session is a helper script for Bitwarden CLI session management.
It solves a common workflow problem: getting a valid BW_SESSION quickly, reusing it when still valid, and refreshing it only when needed.
- Reuses a valid
BW_SESSIONinstead of rotating on every run. - Supports explicit rotation via
--refresh-session. - Can emit shell-specific assignment output for
bash,zsh, orfish. - Can run
bw ...commands with a valid session automatically. - Reads Bitwarden API credentials and master password from macOS Keychain so credentials are not stored in plaintext dotfiles.
Use this project when you use bw frequently from terminal automation and want predictable, low-friction auth/session behavior.
- macOS (uses the built-in
securitycommand for Keychain access) bw(Bitwarden CLI)jq- A Bitwarden account with API key credentials
Check dependencies:
command -v bw
command -v jq
command -v security- Sign in to the Bitwarden Web Vault at https://vault.bitwarden.com.
- Open your profile menu (top right) and choose
Account settings. - Go to the
Securitysection, then open theKeyspage. - In the
API Keysection, clickView API key(orGenerate API keyif one does not exist yet). - Complete the identity verification prompt if Bitwarden asks for your master password or 2FA.
- Copy and save both values shown:
client_idclient_secret
bw-session.sh expects these Keychain service names:
BW_CLIENTIDBW_CLIENTSECRETBW_MASTER_PASSWORD
Use secure prompts (example in bash/zsh):
read -rs BWSEC; printf '\n'; security add-generic-password -a "$USER" -s BW_CLIENTID -w "$BWSEC" -U; unset BWSEC
read -rs BWSEC; printf '\n'; security add-generic-password -a "$USER" -s BW_CLIENTSECRET -w "$BWSEC" -U; unset BWSEC
read -rs BWSEC; printf '\n'; security add-generic-password -a "$USER" -s BW_MASTER_PASSWORD -w "$BWSEC" -U; unset BWSECUse secure prompts (example in fish):
read -s BWSEC; echo; security add-generic-password -a "$USER" -s BW_CLIENTID -w "$BWSEC" -U; set -e BWSEC
read -s BWSEC; echo; security add-generic-password -a "$USER" -s BW_CLIENTSECRET -w "$BWSEC" -U; set -e BWSEC
read -s BWSEC; echo; security add-generic-password -a "$USER" -s BW_MASTER_PASSWORD -w "$BWSEC" -U; set -e BWSEC./bw-session.shExamples:
./bw-session.sh --bash
./bw-session.sh --zsh
./bw-session.sh --fish./bw-session.sh --versionbash/zsh:
eval "$(./bw-session.sh)"fish:
eval (./bw-session.sh --fish)./bw-session.sh --refresh-session./bw-session.sh list items
./bw-session.sh get item <item-id>
./bw-session.sh --refresh-session list items- Do not commit real credentials, session tokens, or exported environment values.
- Do not paste
BW_SESSIONvalues into issue trackers or chat. - Keep Keychain as the secret source of truth for local machine use.
- Consider rotating Bitwarden API key if you suspect exposure.
bw-session.sh: main scriptREADME.md: setup and usage documentation