<p>I am trying to get the namespace of the currently used Kubernetes context using <code>kubectl</code>.</p>

<p>I know there is a command <code>kubectl config get-contexts</code> but I see that it cannot output in json/yaml. The only script I've come with is this:</p>

<pre class="lang-sh prettyprint-override"><code>kubectl config get-contexts --no-headers | grep '*' | grep -Eo '\S+$'
</code></pre>

Mikhail Golubtsov
<p>This works if you have a namespace selected in your context:</p>
<pre><code>kubectl config view --minify -o jsonpath='{..namespace}'
</code></pre>
<p>Also, <a href="https://github.com/jonmosco/kube-ps1" rel="noreferrer">kube-ps1</a> can be used to display your current context and namespace in your shell prompt.</p>
