<p>I'm trying to code a bash function to get and decode a kubernetes secret's data in just one short command. My idea is to have something like <code>kgsecd -n &lt;namespace&gt; &lt;secret-name&gt; &lt;secret-data&gt;</code> that is just mapped to <code>kubectl get secret -n &lt;namespace&gt; &lt;secret-name&gt; -o &quot;jsonpath={.data.&lt;secret-data&gt;}&quot; | base64 -d</code>. I have already coded it as follows:</p>
<pre class="lang-sh prettyprint-override"><code>  kgsecd() {
    secretData=&quot;${@: -1}&quot;
    kubectlParams=(${@:1:-1})
    echo &quot;kubectl get secret ${kubectlParams} -o \&quot;jsonpath={.data.$secretData}\&quot; | base64 -d&quot;
    kubectl get secret &quot;${kubectlParams}&quot; -o &quot;jsonpath={.data.$secretData}&quot; | base64 -d;
  }
</code></pre>
<p>However, I'm struggling to make it work as when I call it, it doesn't show any output in the terminal (apart from that from the <code>echo</code> sentence), but if I copy&amp;paste and executed the output of the echo sentence, it works as expected. Let me show you what I mean with an example:</p>
<pre class="lang-sh prettyprint-override"><code>$&gt; kgsecd -n my-ns my-secret secret-data
kubectl get secret -n my-ns my-secret -o &quot;jsonpath={.data.secret-data}&quot; | base64 -d
$&gt;
</code></pre>
<p>But when I execute <code>kubectl get secret -n my-ns my-secret -o &quot;jsonpath={.data.secret-data}&quot; | base64 -d</code>, I get the expected result.</p>

beni0888
<p>If you are using <code>bash</code> try the following, only change is the way <code>kubectlParams</code> is assigned. Here, <code>kubectlParams</code> is assigned with 1st arg to penultimate(<code>$#-1</code>) arguments.</p>
<p>Also, <code>&quot;${kubectlParams}&quot;</code> if quoted, then will be considered as a command. Eg: <code>-n my-ns my-secret</code> would be consider as as a single string. and  it would be considered as a single string , and that string is argument to <code>kubectl</code>. kubectl understand <code>-n</code> , <code>my-ns</code>, <code>my-secret</code> , but not <code>-n my-ns mysecret</code>.</p>
<pre><code>kgsecd() {
 secretData=&quot;${@: -1}&quot;
 kubectlParams=${@:1:$#-1}

 echo &quot;kubectl get secret ${kubectlParams} -o \&quot;jsonpath={.data.$secretData}\&quot; | base64 -d&quot;
 kubectl get secret ${kubectlParams} -o &quot;jsonpath={.data.$secretData}&quot; | base64 -d
}
</code></pre>
<p>Execution output:</p>
<pre><code>#test secret created:
kubectl create secret  generic my-secret -n my-ns --from-literal=secret-data=helloooooo


#function output
kgsecd -n my-ns my-secret secret-data
kubectl get secret -n my-ns my-secret -o &quot;jsonpath={.data.secret-data}&quot; | base64 -d
helloooooo
</code></pre>
<p>#manual command execution output:</p>
<pre><code>kubectl get secret -n my-ns my-secret -o &quot;jsonpath={.data.secret-data}&quot; | base64 -d
helloooooo
</code></pre>
<p><code>zsh</code> solution(see comments by OP):</p>
<pre><code>kgsecd() { kubectl get secret ${@:1:-1} -o &quot;jsonpath={.data.${@: -1}}&quot; | base64 -d }
</code></pre>
