<h1>Question</h1>
<p>Given this single-line string:</p>
<pre><code>PG_USER=postgres PG_PORT=1234 PG_PASS=icontain=and*symbols
</code></pre>
<p>What would be the right way to assign each value to its designated variable so that I can use it afterward?</p>
<hr />
<h1>Context</h1>
<p>I'm parsing the context of a k8s secret within a <code>CronJob</code> so that I can periodically call a Stored Procedure in our Postgres database.</p>
<p>To do so, I plan on using:</p>
<pre class="lang-sh prettyprint-override"><code>PG_OUTPUT_VALUE=$(PGPASSWORD=$PG_PASSWD psql -qtAX -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DATABASE -c $PG_TR_CLEANUP_QUERY)

echo $PG_OUTPUT_VALUE
</code></pre>
<p>The actual entire helm chart I'm currently trying to fix looks like this:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ template &quot;fullname&quot; $ }}-tr-cleanup-cronjob
spec:
  concurrencyPolicy: Forbid
  schedule: &quot;* * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
          - name: postgres
            secret:
              secretName: {{ template &quot;fullname&quot; $ }}-postgres
          containers:
          - name: {{ template &quot;fullname&quot; $ }}-tr-cleanup-pod
            image: postgres:12-alpine
            imagePullPolicy: Always
            env:
              - name: PG_PROPS
                valueFrom:
                  secretKeyRef:
                    name: {{ template &quot;fullname&quot; $ }}-postgres
                    key: postgres.properties
            command:
              - /bin/sh
              - -c
              - echo &quot;props:&quot; &amp;&amp; echo $PG_PROPS &amp;&amp; PG_USER=$(grep &quot;^PG_USER=&quot; | cut -d&quot;=&quot; -f2-) &amp;&amp; echo $PG_USER &amp;&amp; PG_TR_CLEANUP_QUERY=&quot;SELECT something FROM public.somewhere;&quot; &amp;&amp; echo $PG_TR_CLEANUP_QUERY &amp;&amp; PG_OUTPUT_VALUE=$(PGPASSWORD=$PG_PASSWD psql -qtAX -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DATABASE -c $PG_TR_CLEANUP_QUERY) &amp;&amp; echo PG_OUTPUT_VALUE
            volumeMounts:
              - name: postgres
                mountPath: /etc/secrets/postgres
</code></pre>
<h3>Current approach</h3>
<p>As you can see, I'm currently using:</p>
<pre class="lang-sh prettyprint-override"><code>PG_USER=$(grep &quot;^PG_USER=&quot; | cut -d&quot;=&quot; -f2-)
</code></pre>
<p>That is because I initially thought the secret would be output on multiple lines, but it turns out that I was wrong. The <code>echo $PG_USER</code> displays an empty string.</p>

payne
<h1>Option 1</h1>
<p>This function can be reused to assign each variable individually:</p>
<pre class="lang-sh prettyprint-override"><code>extract() {
  echo &quot;$INPUT&quot; | grep -o &quot;$1=.*&quot; | cut -d&quot; &quot; -f1 | cut -d&quot;=&quot; -f2- ;
}
</code></pre>
<p>And to use it:</p>
<pre class="lang-sh prettyprint-override"><code>PG_USER=$(extract PG_USER)
PG_PORT=$(extract PG_PORT)
PG_PASS=$(extract PG_PASS)
</code></pre>
<hr />
<h1>Option 2</h1>
<p>Another potential solution, <strong>with a security concern</strong>, is to simply use:</p>
<pre class="lang-sh prettyprint-override"><code>eval &quot;$INPUT&quot;
</code></pre>
<p>It should only be used if you have validated the input.</p>
<hr />
<h1>Contextual complete answer</h1>
<p>And because I've presented the k8s context in the question, here is the answer as plugged into that solution.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ template &quot;fullname&quot; $ }}-cronjob
spec:
  concurrencyPolicy: Forbid
  schedule: &quot;* * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
          - name: postgres
            secret:
              secretName: {{ template &quot;fullname&quot; $ }}-postgres
          containers:
          - name: {{ template &quot;fullname&quot; $ }}-cronjob-pod
            image: postgres:12-alpine
            imagePullPolicy: Always
            env:
              - name: PG_PROPS
                valueFrom:
                  secretKeyRef:
                    name: {{ template &quot;fullname&quot; $ }}-postgres
                    key: postgres.properties
            command:
              - /bin/sh
              - -c
              - &gt;-
                extract() { echo &quot;$PG_PROPS&quot; | grep -o &quot;$1=.*&quot; | cut -d&quot; &quot; -f1 | cut -d&quot;=&quot; -f2- ; } &amp;&amp;

                export PGHOST=$(extract PG_HOST) &amp;&amp;
                export PGPORT=$(extract PG_PORT) &amp;&amp;
                export PGDATABASE=$(extract PG_DATABASE) &amp;&amp;
                export PGUSER=$(extract PG_USER) &amp;&amp;

                PG_SCHEMA=$(extract PG_SCHEMA) &amp;&amp;
                PG_QUERY=&quot;SELECT tenant_schema FROM $PG_SCHEMA.tenant_schema_mappings;&quot; &amp;&amp;

                PGPASSWORD=$(extract PG_PASSWD) psql --echo-all -c &quot;$PG_QUERY&quot;
            volumeMounts:
              - name: postgres
                mountPath: /etc/secrets/postgres
</code></pre>
