Secure API Key Management for AI Tools
Learn the most practical and secure ways to manage API keys for AI tools and MCP servers across different shells like Bash, Zsh, and Fish.

When setting up AI tools like MCP servers, you often need to store sensitive API keys as environment variables. Here is the most practical way to do it for Bash/Zsh and Fish shell.
1. For Zsh or Bash (.zshrc / .bashrc)
Most macOS users (on Zsh) or Linux users (on Bash) store variables in a hidden configuration file. These files are read every time a new terminal window opens.
-
Open your config file:
- Zsh:
nano ~/.zshrc - Bash:
nano ~/.bash_profile(or~/.bashrc)
- Zsh:
-
Add your key at the bottom:
bash export GITHUB_MCP_PAT="ghp_your_token_here" export CONTEXT7_API_KEY="c7_your_key_here" -
Save and Exit: Press
Ctrl+O,Enter, thenCtrl+X. -
Activate changes: Run
source ~/.zshrc(or restart your terminal).
2. For Fish Shell (Universal Variables)
Fish shell handles things differently. Instead of editing a text file, you can set a Universal Variable directly in the command line. This is cleaner and more secure.
-
Run the command once:
fish set -Ux GITHUB_MCP_PAT ghp_your_token_here set -Ux CONTEXT7_API_KEY c7_your_key_here -
Why this is better:
- Persistence: It survives restarts without needing a “source” command.
- No File Bloat: It is stored in Fish’s internal database, not in your
config.fishtext file. - Security: If you share your
config.fishonline, your api keys stay hidden.
Summary Table
| Feature | Zsh / Bash | Fish (Universal) |
|---|---|---|
| Storage | Plain text file (.zshrc) |
Internal binary database |
| Command | export KEY="value" |
set -Ux KEY value |
| Requires Restart? | Yes (or source) |
No (immediate) |
| Best for Dotfiles | Manual management | Automatic isolation |
Never commit your .zshrc or config.fish to a public GitHub repository if you have written your API keys directly into them.