Skip to content

Storing Sensitive Values in .zshrc with macOS Keychain

macOS Keychain security

Storing tokens and passwords directly in ~/.zshrc means they sit on disk in plaintext. macOS Keychain provides a built-in, encrypted alternative. Here’s how to use it.

Use the security CLI to add a value to your Keychain:

Terminal window
security add-generic-password -a "$USER" -s 'my_secret_name' -w 'SECRET_VALUE'
  • -a "$USER" — associates the entry with your macOS user account.
  • -s 'my_secret_name' — a label to identify the secret (e.g., GITHUB_TOKEN).
  • -w 'SECRET_VALUE' — the actual secret value.

To retrieve the value later:

Terminal window
security find-generic-password -a "$USER" -s 'my_secret_name' -w

This prints the secret to stdout, making it easy to capture in a variable.

Export the secret as an environment variable by adding this to ~/.zshrc:

Terminal window
export GITHUB_TOKEN=$(security find-generic-password -a "$USER" -s "GITHUB_TOKEN" -w)
TaskCommand
Storesecurity add-generic-password -a "$USER" -s 'name' -w 'value'
Retrievesecurity find-generic-password -a "$USER" -s 'name' -w
UpdateDelete and re-add, or use -U flag to update in place
Deletesecurity delete-generic-password -a "$USER" -s 'name'

That’s it — no more plaintext secrets in your .zshrc.