# Select \`kubectl\` context with environment variable?

**URL:** <https://discuss.kubernetes.io/t/select-kubectl-context-with-environment-variable/24573>\
**Category:** General Discussions\
**Created:** [June 20, 2023, 10:17pm UTC](https://discuss.kubernetes.io/t/select-kubectl-context-with-environment-variable/24573 "2023-06-20T22:17:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkil](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/tkil/32/12751_2.png) [@tkil](https://discuss.kubernetes.io/u/tkil)\
**Post date:** [June 20, 2023, 10:17pm UTC](https://discuss.kubernetes.io/t/select-kubectl-context-with-environment-variable/24573/1 "2023-06-20T22:17:08Z")

</div>

Hello!

Is there a way to ask `kubectl` to use a specific context via environment variable, rather than through `--context`?

That is, instead of: `kubectl --context foo ...`  
I’d like to be able to do: `KUBECTL_CONTEXT=foo kubectl ...`

(Or whatever – the name of the environment variable isn’t important.)

Similar to both Helm (`HELM_KUBECONTEXT`) and other tools, e.g., `aws-cli` (`AWS_PROFILE` vs. `--profile`).

Apologies if I missed an existing feature, but it looks like there are only 4 documented [environment variables](https://kubernetes.io/docs/reference/kubectl/kubectl/#environment-variables) ?

(There’s `KUBECONFIG` which can be used for something similar (by copying content from the shared config file and specifying the current context). But that involves creating a separate config file, when all I need in the current case is to use a specific context out of the existing shared `~/.kube/config` file.)

Thank you!

---

<div class="post-metadata">

**Author:** ![DaVla](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/davla/32/12265_2.png) [@DaVla](https://discuss.kubernetes.io/u/DaVla)\
**Post date:** [June 21, 2023, 11:37am UTC](https://discuss.kubernetes.io/t/select-kubectl-context-with-environment-variable/24573/2 "2023-06-21T11:37:38Z")

</div>

I think to achieve the desired behavior of specifying a specific context for kubectl without using the --context flag.  
The KUBECONFIG environment variable allows you to define the path to the Kubernetes configuration file that kubectl should use. By default, kubectl looks for the configuration file at ~/.kube/config. However, you can override this by setting the KUBECONFIG environment variable.  
Trying to [outsource technical support services](https://www.helpware.com/dedicated-technical-support-outsourcing) in this case may be a good idea)
