<p>I'm writing a utility script to retrieve and parse some kubernetes config maps, and typically I'll be authenticated using an oidc token.</p>
<p>In the event when I'm not authenticated, how can I make my <code>kubectl</code> command exit with a failure rather than prompt for a username and password?</p>
<p>Here's my current implementation:</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/bash

# Prompts me with &quot;Please enter Username:&quot;, and I'd like to exit instead.
kubectl get cm -n my-namespace
</code></pre>
<p>Thanks in advance.</p>

<p>You may use <code>&lt;/dev/null</code> in your command to <a href="https://stackoverflow.com/a/19955494/6309601">close the std input for the command</a>. Check the example below, where <code>kubectl</code> would print the result normally if things are fine(not prompted), else it will print error text.</p>
<p>Using a known good <code>kubeconfig</code> file:</p>
<pre><code>kubectl  get pod --kubeconfig good_kube_config &lt;/dev/null
No resources found in default namespace.
</code></pre>
<p>Using a <code>kubeconfig</code> with misconfigured username:</p>
<pre><code>kubectl  get pod --kubeconfig bad_kube_config &lt;/dev/null
Please enter Username: error: EOF
</code></pre>
<p>You can use something like the below in your script, note that this would print an error on all the failures regardless of their relation with user/pass prompt.</p>
<pre><code>if !  kubectl get cm -n my-namespace &lt;/dev/null 2&gt;/dev/null;then
      echo &quot;Error: Somthing is wrong!&quot;
      exit 1;
fi
</code></pre>
<p>If you want to be very specific to user/pass error, then suggest you use a kubectl get command to run a test by greping &quot;Username&quot; string, then proceed.</p>
<p>Note:  To simulate the same prompt, I renamed the user name in my kubeconfig file.</p>
