<p>I have observed that <code>kubectl</code> inserts an additional <code>\</code> to linefeed characters when using the <code>--from-literal</code> option. It works as expected when loading &quot;the same&quot; content from a file. Clearly, there must be a difference because the stdout looks different but I fail to see why.</p>
<pre><code>echo &quot;api_key&quot; &gt;&gt; .tmp
echo &quot;api_value&quot; &gt;&gt; .tmp
cat -e .tmp
kubectl delete secrets api-env
kubectl create secret generic api-env --from-file=.api=.tmp
rm .tmp
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key$
#api_value$
#secret &quot;api-env&quot; deleted
#secret/api-env created
#{
#  &quot;.api&quot;: &quot;api_key\napi_value\n&quot;
#}

</code></pre>
<p>The commands above create a single linefeed character on each line. Demonstrated by <code>cat -e</code> there are two linefeed characters in the file, each at the end.</p>
<p>Doing the same using a string results in the <code>\n</code> to be escaped.</p>
<pre><code>api=&quot;api_key\napi_value\n&quot;
echo $api
kubectl delete secrets api-env
kubectl create secret generic api-env --from-literal=.api=$api
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key\napi_value\n
#secret &quot;api-env&quot; deleted
#secret/api-env created
#{
#  &quot;.api&quot;: &quot;api_key\\napi_value\\n&quot;
#}
</code></pre>
<p>The <code>echo</code> command shows the string as it was supplied to the variable, however after loading that to kubernetes the <code>\n</code> are escaped and the content is considered to be a single line.</p>
<p>This is important because in several instances where I am working with <code>kubectl</code> I am not allowed to write to the local file system.</p>
<p>What is happening here and how to stop kubernetes from escaping the <code>\n</code> character?</p>
<p>Environment:</p>
<ul>
<li>zsh 5.8 (x86_64-apple-darwin21.0)</li>
<li>Darwin Kernel Version 21.4.0: root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64</li>
<li>kubectl Client:&quot;v1.20.10&quot;</li>
<li>kubectl Server: &quot;v1.23.3&quot;</li>
<li>minikube version: v1.25.2</li>
</ul>

RndmSymbl
<p>When you use <code>echo $api</code>, <code>echo</code> itself changes the contents: On POSIX-compliant shells with XSI extensions enabled (and while zsh isn't POSIX-compliant in general, it <em>does</em> implement this aspect), the <code>\n</code>s are replaced with literal newlines.</p>
<p>That <em>doesn't</em> happen with <code>--from-literal=.api=$api</code>; there, your <code>\n</code>s are still two-character sequences, first a backslash, then a <code>n</code>.</p>
<p>Given that you're on a shell that supports using <code>$'\n'</code> as a way to represent a newline literal directly, consider <code>--from-literal=.api=&quot;api_key&quot;$'\n'&quot;api_value&quot;</code></p>
