Answers: <p>As the <a href="https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object" rel="nofollow noreferrer">docs</a> says,  you can only use UTF-8 strings. Try changing your ConfigMap to this</p>
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: time-config
  data:
    TIME_FREQ: &quot;6&quot;
  </code></pre>
  <p>And leave your pod template as is. I just tested it, and it works. :)</p>
  <p>send a message if you need more help</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-09-01T12:18:04.420Z
Answers: Thanks for the reply. Actually, I forgot to put the number between quotes in the question, fixed. It wouldn't even let me create the CM without it. Thought, I tried again and the env var still wouldn't pass through to the sleep command. I am using minikube for testing and the exercise is from kodekloud, both environments the same result. sleep is without argument and sends into log sh: sleep: missing operand
Answers: user27155638
Answers: CC BY-SA 4.0
Answers: 2024-09-01T12:22:56.497Z
Answers: @user27155638 Please provide full yaml output of the pod manifest so i could get more context.
  also logs output of the pod.
  it seems that it is a problem with the shell kodekloud provides maybe.
Answers: Eladik
Answers: CC BY-SA 4.0
Answers: 2024-09-01T12:37:10.697Z
Answers: Please, see the edits in question with get and describe outputs. In kubectl logs, there is only endless stream of `sh: sleep: missing operand`
Answers: user27155638
Answers: CC BY-SA 4.0
Answers: 2024-09-01T11:56:19.630Z
Answers: 2024-09-01T12:04:13.613Z
Answers: 2024-09-01T12:04:13.613Z
Answers: Eladik
Answers: <p>I figured out why it's not working for me. I was using heredoc to create the file and didn't escape the variable. For refrence, here's some info <a href="https://stackoverflow.com/questions/56752494/variable-isnt-initialized-in-script-passed-through-heredoc">https://stackoverflow.com/questions/56752494/variable-isnt-initialized-in-script-passed-through-heredoc</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-09-02T18:36:38.620Z
Answers: Can you improve your self-answer with some explanation of how the SO post helped and some code?
Answers: Peter Turner
Answers: CC BY-SA 4.0
Answers: 2024-09-02T10:58:10.050Z
Answers: 2024-09-02T10:58:10.050Z
Answers: user27155638
Body: <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: time-config
  data:
    TIME_FREQ: &quot;6&quot;
  ---
  apiVersion: v1
  kind: Pod
  metadata:
    name: time-check
  spec:
    containers:
    - name: time-check
      image: busybox:latest
      command: [&quot;/bin/sh&quot;]
      args: [&quot;-c&quot;,&quot;while true; do date; sleep $TIME_FREQ;done &gt;&gt; /opt/data/time/time-check.log&quot;]
      env:
        - name: TIME_FREQ
          valueFrom:
            configMapKeyRef:
              name: time-config
              key: TIME_FREQ
      volumeMounts:
        - name: log-volume
          mountPath: /opt/data/time
    volumes:
    - name: log-volume
      emptyDir: {}
  </code></pre>
  <p>According to <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#use-environment-variables-to-define-arguments" rel="nofollow noreferrer">documentation</a>, to use variable you should put it in parentheses, however, that gives me:</p>
  <pre><code>TIME_FREQ: command not found
  sleep: missing operand
  </code></pre>
  <p>If I use only $TIME_FREQ, the variable does not expand at all and <code>sleep</code> only complains about missing operand. I've tried curly braces, but didn't work, too.</p>
  <p>If I set the variable locally, the config will accept it and the command runs ok, with variable defined as $TIME_FREQ.</p>
  <p>Why does it not work?</p>
  <p>Output from get:</p>
  <pre><code>apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {&quot;apiVersion&quot;:&quot;v1&quot;,&quot;kind&quot;:&quot;Pod&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;time-check&quot;,&quot;namespace&quot;:&quot;default&quot;},&quot;spec&quot;:{&quot;containers&quot;:[{&quot;args&quot;:[&quot;-c&quot;,&quot;while true; do date; sleep ;done \u003e\u003e /opt/data/time/time-check.log&quot;],&quot;command&quot;:[&quot;/bin/sh&quot;],&quot;env&quot;:[{&quot;name&quot;:&quot;TIME_FREQ&quot;,&quot;valueFrom&quot;:{&quot;configMapKeyRef&quot;:{&quot;key&quot;:&quot;TIME_FREQ&quot;,&quot;name&quot;:&quot;time-config&quot;}}}],&quot;image&quot;:&quot;busybox:latest&quot;,&quot;name&quot;:&quot;time-check&quot;,&quot;volumeMounts&quot;:[{&quot;mountPath&quot;:&quot;/opt/data/time&quot;,&quot;name&quot;:&quot;log-volume&quot;}]}],&quot;volumes&quot;:[{&quot;emptyDir&quot;:{},&quot;name&quot;:&quot;log-volume&quot;}]}}
    creationTimestamp: &quot;2024-09-01T12:07:41Z&quot;
    name: time-check
    namespace: default
    resourceVersion: &quot;28228&quot;
    uid: 281e2819-df90-41c0-a5c4-b7203ed2ebec
  spec:
    containers:
    - args:
      - -c
      - while true; do date; sleep ;done &gt;&gt; /opt/data/time/time-check.log
      command:
      - /bin/sh
      env:
      - name: TIME_FREQ
        valueFrom:
          configMapKeyRef:
            key: TIME_FREQ
            name: time-config
      image: busybox:latest
      imagePullPolicy: Always
      name: time-check
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /opt/data/time
        name: log-volume
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: kube-api-access-bm6wc
        readOnly: true
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    nodeName: minikube
    preemptionPolicy: PreemptLowerPriority
    priority: 0
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - emptyDir: {}
      name: log-volume
    - name: kube-api-access-bm6wc
      projected:
        defaultMode: 420
        sources:
        - serviceAccountToken:
            expirationSeconds: 3607
            path: token
        - configMap:
            items:
            - key: ca.crt
              path: ca.crt
            name: kube-root-ca.crt
        - downwardAPI:
            items:
            - fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
              path: namespace
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: &quot;2024-09-01T12:07:49Z&quot;
      status: &quot;True&quot;
      type: PodReadyToStartContainers
    - lastProbeTime: null
      lastTransitionTime: &quot;2024-09-01T12:07:41Z&quot;
      status: &quot;True&quot;
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: &quot;2024-09-01T12:07:49Z&quot;
      status: &quot;True&quot;
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: &quot;2024-09-01T12:07:49Z&quot;
      status: &quot;True&quot;
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: &quot;2024-09-01T12:07:41Z&quot;
      status: &quot;True&quot;
      type: PodScheduled
    containerStatuses:
    - containerID: docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
      image: busybox:latest
      imageID: docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
      lastState: {}
      name: time-check
      ready: true
      restartCount: 0
      started: true
      state:
        running:
          startedAt: &quot;2024-09-01T12:07:49Z&quot;
    hostIP: 192.168.49.2
    hostIPs:
    - ip: 192.168.49.2
    phase: Running
    podIP: 10.244.0.6
    podIPs:
    - ip: 10.244.0.6
    qosClass: BestEffort
    startTime: &quot;2024-09-01T12:07:41Z&quot;
  </code></pre>
  <p>And describe:</p>
  <pre><code>Name:             time-check
  Namespace:        default
  Priority:         0
  Service Account:  default
  Node:             minikube/192.168.49.2
  Start Time:       Sun, 01 Sep 2024 14:07:41 +0200
  Labels:           &lt;none&gt;
  Annotations:      &lt;none&gt;
  Status:           Running
  IP:               10.244.0.6
  IPs:
    IP:  10.244.0.6
  Containers:
    time-check:
      Container ID:  docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
      Image:         busybox:latest
      Image ID:      docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
      Port:          &lt;none&gt;
      Host Port:     &lt;none&gt;
      Command:
        /bin/sh
      Args:
        -c
        while true; do date; sleep ;done &gt;&gt; /opt/data/time/time-check.log
      State:          Running
        Started:      Sun, 01 Sep 2024 14:07:49 +0200
      Ready:          True
      Restart Count:  0
      Environment:
        TIME_FREQ:  &lt;set to the key 'TIME_FREQ' of config map 'time-config'&gt;  Optional: false
      Mounts:
        /opt/data/time from log-volume (rw)
        /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bm6wc (ro)
  Conditions:
    Type                        Status
    PodReadyToStartContainers   True 
    Initialized                 True 
    Ready                       True 
    ContainersReady             True 
    PodScheduled                True 
  Volumes:
    log-volume:
      Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
      Medium:     
      SizeLimit:  &lt;unset&gt;
    kube-api-access-bm6wc:
      Type:                    Projected (a volume that contains injected data from multiple sources)
      TokenExpirationSeconds:  3607
      ConfigMapName:           kube-root-ca.crt
      ConfigMapOptional:       &lt;nil&gt;
      DownwardAPI:             true
  QoS Class:                   BestEffort
  Node-Selectors:              &lt;none&gt;
  Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                               node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
  Events:
    Type    Reason     Age   From               Message
    ----    ------     ----  ----               -------
    Normal  Scheduled  22m   default-scheduler  Successfully assigned default/time-check to minikube
    Normal  Pulling    22m   kubelet            Pulling image &quot;busybox:latest&quot;
    Normal  Pulled     22m   kubelet            Successfully pulled image &quot;busybox:latest&quot; in 7.077s (7.077s including waiting). Image size: 4261574 bytes.
    Normal  Created    22m   kubelet            Created container time-check
    Normal  Started    22m   kubelet            Started container time-check
  </code></pre>
  
Tags: <kubernetes><shell-script><environment-variables>
Title: Passing ENV VAR via ConfigMap does not work
UserDisplayName: user27155638
ThreadText: # Passing ENV VAR via ConfigMap does not work
  
  (Asked by: username_0 on 2024-09-01)
  
  ```
  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: time-config
  data:
    TIME_FREQ: "6"
  ---
  apiVersion: v1
  kind: Pod
  metadata:
    name: time-check
  spec:
    containers:
    - name: time-check
      image: busybox:latest
      command: ["/bin/sh"]
      args: ["-c","while true; do date; sleep $TIME_FREQ;done >> /opt/data/time/time-check.log"]
      env:
        - name: TIME_FREQ
          valueFrom:
            configMapKeyRef:
              name: time-config
              key: TIME_FREQ
      volumeMounts:
        - name: log-volume
          mountPath: /opt/data/time
    volumes:
    - name: log-volume
      emptyDir: {}
  
  ```
  
  According to [documentation](https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#use-environment-variables-to-define-arguments), to use variable you should put it in parentheses, however, that gives me:
  
  ```
  TIME_FREQ: command not found
  sleep: missing operand
  
  ```
  
  If I use only $TIME\_FREQ, the variable does not expand at all and `sleep` only complains about missing operand. I've tried curly braces, but didn't work, too.
  
  If I set the variable locally, the config will accept it and the command runs ok, with variable defined as $TIME\_FREQ.
  
  Why does it not work?
  
  Output from get:
  
  ```
  apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"time-check","namespace":"default"},"spec":{"containers":[{"args":["-c","while true; do date; sleep ;done \u003e\u003e /opt/data/time/time-check.log"],"command":["/bin/sh"],"env":[{"name":"TIME_FREQ","valueFrom":{"configMapKeyRef":{"key":"TIME_FREQ","name":"time-config"}}}],"image":"busybox:latest","name":"time-check","volumeMounts":[{"mountPath":"/opt/data/time","name":"log-volume"}]}],"volumes":[{"emptyDir":{},"name":"log-volume"}]}}
    creationTimestamp: "2024-09-01T12:07:41Z"
    name: time-check
    namespace: default
    resourceVersion: "28228"
    uid: 281e2819-df90-41c0-a5c4-b7203ed2ebec
  spec:
    containers:
    - args:
      - -c
      - while true; do date; sleep ;done >> /opt/data/time/time-check.log
      command:
      - /bin/sh
      env:
      - name: TIME_FREQ
        valueFrom:
          configMapKeyRef:
            key: TIME_FREQ
            name: time-config
      image: busybox:latest
      imagePullPolicy: Always
      name: time-check
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /opt/data/time
        name: log-volume
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: kube-api-access-bm6wc
        readOnly: true
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    nodeName: minikube
    preemptionPolicy: PreemptLowerPriority
    priority: 0
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - emptyDir: {}
      name: log-volume
    - name: kube-api-access-bm6wc
      projected:
        defaultMode: 420
        sources:
        - serviceAccountToken:
            expirationSeconds: 3607
            path: token
        - configMap:
            items:
            - key: ca.crt
              path: ca.crt
            name: kube-root-ca.crt
        - downwardAPI:
            items:
            - fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
              path: namespace
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: "2024-09-01T12:07:49Z"
      status: "True"
      type: PodReadyToStartContainers
    - lastProbeTime: null
      lastTransitionTime: "2024-09-01T12:07:41Z"
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: "2024-09-01T12:07:49Z"
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: "2024-09-01T12:07:49Z"
      status: "True"
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: "2024-09-01T12:07:41Z"
      status: "True"
      type: PodScheduled
    containerStatuses:
    - containerID: docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
      image: busybox:latest
      imageID: docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
      lastState: {}
      name: time-check
      ready: true
      restartCount: 0
      started: true
      state:
        running:
          startedAt: "2024-09-01T12:07:49Z"
    hostIP: 192.168.49.2
    hostIPs:
    - ip: 192.168.49.2
    phase: Running
    podIP: 10.244.0.6
    podIPs:
    - ip: 10.244.0.6
    qosClass: BestEffort
    startTime: "2024-09-01T12:07:41Z"
  
  ```
  
  And describe:
  
  ```
  Name:             time-check
  Namespace:        default
  Priority:         0
  Service Account:  default
  Node:             minikube/192.168.49.2
  Start Time:       Sun, 01 Sep 2024 14:07:41 +0200
  Labels:           <none>
  Annotations:      <none>
  Status:           Running
  IP:               10.244.0.6
  IPs:
    IP:  10.244.0.6
  Containers:
    time-check:
      Container ID:  docker://abd378ce0519222c43fc194ba6952475176fba2bb37ee71a5ac9f6838239d2cb
      Image:         busybox:latest
      Image ID:      docker-pullable://busybox@sha256:82742949a3709938cbeb9cec79f5eaf3e48b255389f2dcedf2de29ef96fd841c
      Port:          <none>
      Host Port:     <none>
      Command:
        /bin/sh
      Args:
        -c
        while true; do date; sleep ;done >> /opt/data/time/time-check.log
      State:          Running
        Started:      Sun, 01 Sep 2024 14:07:49 +0200
      Ready:          True
      Restart Count:  0
      Environment:
        TIME_FREQ:  <set to the key 'TIME_FREQ' of config map 'time-config'>  Optional: false
      Mounts:
        /opt/data/time from log-volume (rw)
        /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bm6wc (ro)
  Conditions:
    Type                        Status
    PodReadyToStartContainers   True 
    Initialized                 True 
    Ready                       True 
    ContainersReady             True 
    PodScheduled                True 
  Volumes:
    log-volume:
      Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
      Medium:     
      SizeLimit:  <unset>
    kube-api-access-bm6wc:
      Type:                    Projected (a volume that contains injected data from multiple sources)
      TokenExpirationSeconds:  3607
      ConfigMapName:           kube-root-ca.crt
      ConfigMapOptional:       <nil>
      DownwardAPI:             true
  QoS Class:                   BestEffort
  Node-Selectors:              <none>
  Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                               node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
  Events:
    Type    Reason     Age   From               Message
    ----    ------     ----  ----               -------
    Normal  Scheduled  22m   default-scheduler  Successfully assigned default/time-check to minikube
    Normal  Pulling    22m   kubelet            Pulling image "busybox:latest"
    Normal  Pulled     22m   kubelet            Successfully pulled image "busybox:latest" in 7.077s (7.077s including waiting). Image size: 4261574 bytes.
    Normal  Created    22m   kubelet            Created container time-check
    Normal  Started    22m   kubelet            Started container time-check
  
  ```
  
  **Tags:** kubernetes, shell-script, environment-variables
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-09-01. Score: 0
  
  As the [docs](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) says, you can only use UTF-8 strings. Try changing your ConfigMap to this
  
  ```
  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: time-config
  data:
    TIME_FREQ: "6"
  
  ```
  
  And leave your pod template as is. I just tested it, and it works. :)
  
  send a message if you need more help
  
  > **Comments:**
  > **username_0**: Thanks for the reply. Actually, I forgot to put the number between quotes in the question, fixed. It wouldn't even let me create the CM without it. Thought, I tried again and the env var still wouldn't pass through to the sleep command. I am using minikube for testing and the exercise is from kodekloud, both environments the same result. sleep is without argument and sends into log sh: sleep: missing operand  
  > **username_1**: @username_0 Please provide full yaml output of the pod manifest so i could get more context. also logs output of the pod. it seems that it is a problem with the shell kodekloud provides maybe.  
  > **username_0**: Please, see the edits in question with get and describe outputs. In kubectl logs, there is only endless stream of `sh: sleep: missing operand`  
  
  ---
  
  ### Answer by username_0 on 2024-09-02. Score: 0
  
  I figured out why it's not working for me. I was using heredoc to create the file and didn't escape the variable. For refrence, here's some info <https://stackoverflow.com/questions/56752494/variable-isnt-initialized-in-script-passed-through-heredoc>
  
  > **Comments:**
  > **username_2**: Can you improve your self-answer with some explanation of how the SO post helped and some code?  
  
  ---
  
  