1

According to the [documentation][1] Kubernetes variables are expanded using the previous defined environment variables in the container using the syntax $(VAR_NAME). The variable can be used in the container's entrypoint.

For example:

env:
- name: MESSAGE
  value: "hello world"
command: ["/bin/echo"]
args: ["$(MESSAGE)"]

Is this possible though to use bash expansion aka ${Var1:-${Var2}} inside the container's entrypoint for the kubernetes environment variables E.g.

env:
- name: Var1
  value: "hello world"
- name: Var2
  value: "no hello"
command: ['bash', '-c', "echo ${Var1:-$Var2}"]

1 Answer 1

4

Is this possible though to use bash expansion aka ${Var1:-${Var2}} inside the container's entrypoint ?

Yes, by using

command: 
- /bin/bash
- "-c"
- "echo ${Var1:-${Var2}}"

but not otherwise -- kubernetes is not a wrapper for bash, it use the Linux exec system call to launch programs inside the container, and so the only way to get bash behavior is to launch bash

That's also why they chose $() syntax for their environment interpolation so it would be different from the ${} style that a shell would use, although this question comes up so much that one might wish they had not gone with $ anything to avoid further confusing folks

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks daniel! I updateed the question. Will this work also if the variables are kubernetes variables? As stated in the documentation $() syntax is used to get the value of previous defined environment variables in the container
It took you longer to type out that comment and the question update than it would have to try it; yes, env vars are made available to the bash entrypoint, and will be evaluated in their normal bash-y way
Also note that this won't work in an os-less container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.