<p>I have the following Kubernetes job defined in <code>job.yaml</code></p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: pi-$count
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: [&quot;perl&quot;,  &quot;-Mbignum=bpi&quot;, &quot;-wle&quot;, &quot;print bpi(2000)&quot;]
      restartPolicy: Never
  backoffLimit: 4
</code></pre>
<p>I apply this with the following bash script:</p>
<pre><code>for i in {1..3}
do
    kubectl apply -f job.yaml
done
</code></pre>
<p>Is there a way to create 3 jobs here and use an environment variable <code>pi-$count</code> so <code>pi-1</code>, <code>pi-2</code>, <code>pi-3</code> are created?</p>

jipot
<p>Use sed command to replace the job counter (COUNT) and create temporary files for each of the job instances. You can delete all the temporary job yamls after deployment. In this way you can keep your original job yaml file intact for reusing. Let's say I am using your kubernetes job yaml named 'job.yaml'.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: pi-COUNT
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: [&quot;perl&quot;,  &quot;-Mbignum=bpi&quot;, &quot;-wle&quot;, &quot;print bpi(2000)&quot;]
      restartPolicy: Never
  backoffLimit: 4
</code></pre>
<p>Modify your bash script like the following. I'm naming it <code>jobrunner.sh</code>.</p>
<pre class="lang-bash prettyprint-override"><code>#!/usr/bin/bash
for i in {1..3}
do
    sed 's|COUNT|'${i}'|g' job.yaml &gt; job-$i.yaml
    kubectl apply -f job-$i.yaml
done
rm job-*.yaml
</code></pre>
<p>Run this bash script and 3 jobs will be created in your Kubernetes named <code>pi-1</code>, <code>pi-2</code> &amp; <code>pi-3</code>. You can comment out the last part where the temporary job files are being deleted with <code>rm job-*.yaml</code> command if you wish to keep those files.</p>
