{"slug": "i-got-tired-of-passing-profile-on-every-oci-cli-command", "title": "I Got Tired of Passing --profile on Every OCI CLI Command", "summary": "The article describes the problem of managing multiple Oracle Cloud Infrastructure (OCI) tenancies via the CLI, which requires passing both `--compartment-id` and `--profile` flags on every command. The author created a tool called `ocswitch` that simplifies this by using separate config files per tenancy (e.g., `~/.oci/config_prod`) and persisting the active profile across terminal sessions via a state file (`~/.oci/active_profile`). The tool also includes tab completion and an `ocid` function to display the current tenancy and user details for confirmation.", "body_md": "This is a problem you only run into if you work for Oracle directly or you work at a Cloud/DevOps MSP managing OCI for multiple clients. Most people will never touch it. If you are reading this, you are probably not most people.\n\nThe OCI CLI already demands a `--compartment-id`\n\non almost every command. Compartment IDs are long, they are not memorable, and you need a different one depending on what you are looking at. That was already a nightmare before you add multiple tenancies to the picture. Once you are juggling prod, staging, dev, and a handful of client accounts, passing `--profile`\n\non top of `--compartment-id`\n\non every single command becomes the kind of friction that breaks you.\n\nSo I wrote `ocswitch`\n\n.\n\n## What the OCI CLI Actually Gives You\n\nThe OCI CLI reads config from `~/.oci/config`\n\nby default. You can point it at a different file with `OCI_CONFIG_FILE`\n\n, and select a named profile within that file with `OCI_CLI_PROFILE`\n\n. That is the full surface area you have to work with.\n\nThe multi-profile story in the official tooling is basically: put multiple `[PROFILE_NAME]`\n\nsections in one config file and pass `--profile PROFILE_NAME`\n\nevery time. If you forget the flag, you hit whichever profile is in `[DEFAULT]`\n\n. This works but it is tedious, and it is session-scoped - export the env var in one terminal, open another, you are back to default.\n\n`ocswitch`\n\nfixes the persistence problem by writing the active profile to `~/.oci/active_profile`\n\nand restoring it at shell startup.\n\n## How It Works\n\nProfiles are separate config files in `~/.oci/`\n\n:\n\n```\n~/.oci/config_prod\n~/.oci/config_staging\n~/.oci/config_dev\n```\n\nOne file per tenancy, named `config_<profile>`\n\n. When you switch:\n\n```\nocswitch prod\n```\n\nTwo things happen: `~/.oci/active_profile`\n\ngets written with the string `prod`\n\n, and `OCI_CONFIG_FILE`\n\nplus `OCI_CLI_PROFILE`\n\nare exported in the current shell. Every `oci`\n\ncommand you run after that hits the prod tenancy.\n\nThe persistence part lives at the top of the script, outside the function, so it runs every time the shell sources the file:\n\n```\n_oci_state=\"${HOME}/.oci/active_profile\"\nif [[ -f \"$_oci_state\" ]]; then\n  _oci_profile=$(cat \"$_oci_state\")\n  _oci_cfg=\"${HOME}/.oci/config_${_oci_profile}\"\n  if [[ -f \"$_oci_cfg\" ]]; then\n    export OCI_CONFIG_FILE=\"$_oci_cfg\"\n    export OCI_CLI_PROFILE=\"config_${_oci_profile}\"\n  fi\nfi\n```\n\nOpen a new terminal, source kicks in, reads the state file, exports the right vars. The active profile follows you across sessions without any extra steps.\n\n## What It Looks Like\n\n`ocswitch list`\n\nshows all detected profiles (anything matching `~/.oci/config_*`\n\n) with the active one highlighted in Oracle red. `ocswitch clear`\n\nremoves the state file and unsets both env vars.\n\nTab completion is included for both bash and zsh. Hit tab after `ocswitch`\n\nand you get `list`\n\n, `clear`\n\n, and all your profile names.\n\n## The ocid Bonus\n\nThere is a second function in the script: `ocid`\n\n. It reads the active config, pulls the tenancy OCID and user OCID out of it, and calls the OCI IAM API to resolve the human-readable names:\n\n```\nTenant Name: mycompany-production\nTenant ID:   ocid1.tenancy.oc1..aaaa...\nUser Email:  user@example.com\n```\n\nUseful when you have switched between a few profiles and want to confirm exactly which tenancy you are currently pointing at before running something destructive.\n\n## Install\n\n**zsh**\n\n```\ncurl -fsSL https://gist.githubusercontent.com/amaanx86/2621a181e2f4b572a1904d65748d66bd/raw/ocswitch.zsh \\\n  -o ~/.oci/ocswitch.zsh\necho 'source ~/.oci/ocswitch.zsh' >> ~/.zshrc\nsource ~/.zshrc\n```\n\n**bash**\n\n```\ncurl -fsSL https://gist.githubusercontent.com/amaanx86/2621a181e2f4b572a1904d65748d66bd/raw/ocswitch.bash \\\n  -o ~/.oci/ocswitch.bash\necho 'source ~/.oci/ocswitch.bash' >> ~/.bashrc\nsource ~/.bashrc\n```\n\n## Usage\n\n```\nocswitch               # show help + logo\nocswitch list          # list profiles (active highlighted)\nocswitch <profile>     # switch profile (persists across all terminals)\nocswitch clear         # clear active profile\n```\n\n## Profile Setup\n\nEach profile is a standard OCI config file at `~/.oci/config_<profile>`\n\n. The section header inside the file uses the same name as the file itself, not `[DEFAULT]`\n\n:\n\n```\n[config_prod]\nuser=ocid1.user.oc1..aaaa...\nfingerprint=xx:xx:xx:...\ntenancy=ocid1.tenancy.oc1..aaaa...\nregion=us-ashburn-1\nkey_file=~/.oci/oci_api_key.pem\n```\n\nSo `~/.oci/config_staging`\n\nhas `[config_staging]`\n\ninside, `~/.oci/config_dev`\n\nhas `[config_dev]`\n\n, and so on.\n\nThe full source is in [this gist](https://gist.github.com/amaanx86/2621a181e2f4b572a1904d65748d66bd). Two files, one for bash and one for zsh, no external dependencies beyond the OCI CLI itself.\n\nOriginally published at [amaanx86.github.io](https://amaanx86.github.io/blog/ocswitch-oci-profile-switcher/).", "url": "https://wpnews.pro/news/i-got-tired-of-passing-profile-on-every-oci-cli-command", "canonical_source": "https://dev.to/amaanx86/i-got-tired-of-passing-profile-on-every-oci-cli-command-3jpd", "published_at": "2026-05-22 18:12:00+00:00", "updated_at": "2026-05-22 18:34:02.296330+00:00", "lang": "en", "topics": ["developer-tools", "cloud-computing", "open-source"], "entities": ["Oracle", "OCI CLI", "ocswitch"], "alternates": {"html": "https://wpnews.pro/news/i-got-tired-of-passing-profile-on-every-oci-cli-command", "markdown": "https://wpnews.pro/news/i-got-tired-of-passing-profile-on-every-oci-cli-command.md", "text": "https://wpnews.pro/news/i-got-tired-of-passing-profile-on-every-oci-cli-command.txt", "jsonld": "https://wpnews.pro/news/i-got-tired-of-passing-profile-on-every-oci-cli-command.jsonld"}}