
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,950 Views
Hi,
Why this one doesn't print environment variable?
$ podman run -e ACT=greet ubi8/ubi:8.3 echo "Action is $ACT"
Action is
However this one works:
$ podman run -e ACT=greet ubi8/ubi:8.3 printenv ACT
greet
$ podman run -ti -e ACT=greet ubi8/ubi:8.3/bin/bash
I am able to see ACT=greet
What is the difference?
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,928 Views
Hi,
The problem here lies in how variable expansion works. Bash is responsible for expanding variable uses (e.g. $FOO) and replacing their values.
So:
$ export FOO=bar
$ echo $FOO
bar
, when the shell executes the echo statement, it replaces $FOO with bar, and really executes "echo bar", the echo command does not see the variable.
So when you execute:
$ podman run -e ACT=greet ubi8/ubi:8.3 echo "Action is $ACT"
, the shell replaces $ACT with its value. As ACT=greet is a "podman thing", this doesn't set the variable in the shell where podman executes, so the ACT variable is unset, and the shell actually executes:
$ podman run -e ACT=greet ubi8/ubi:8.3 echo "Action is "
When echo is executed inside the container, the ACT variable is set, but nothing that is executed uses the variable.
So your printenv version of the command does work, because the variable is set.
A pattern you might see frequently is:
$ podman run -e ACT=greet ubi8/ubi:8.3 sh -c 'echo "Action is $ACT"'
The single quotes prevent variable expansion from happening, so when podman is executed, $ACT is not replaced by its value. Instead, the container runs the sh -c command. sh -c runs its argument in a shell, so it runs:
$ echo "Action is $ACT"
, where the variable is set, with the behavior you expected.
Cheers,
Álex

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,937 Views
Try this command instead -
podman run --rm ubi8/ubi:8.3 bash && ACT="greet" && echo "Action is $ACT"
env is for environment variable which persists outside of shell. You are trying to echo it as shell variable for which the above method would work.


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,888 Views
Note that this does not achieve pushing ACT into the container. The container starts, opens bash, exits, and finally, local shell essentially evaluates ACT="greed" echo "Action is $ACT".
You can verify this by slightly modifying the container command:
$ podman run --rm ubi8/ubi:8.3 env && ACT="greet" && echo "Action is $ACT"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
container=oci
HOME=/root
HOSTNAME=8af087578cb1
Action is greet
So "ACT=greed" is not passed to the container.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,929 Views
Hi,
The problem here lies in how variable expansion works. Bash is responsible for expanding variable uses (e.g. $FOO) and replacing their values.
So:
$ export FOO=bar
$ echo $FOO
bar
, when the shell executes the echo statement, it replaces $FOO with bar, and really executes "echo bar", the echo command does not see the variable.
So when you execute:
$ podman run -e ACT=greet ubi8/ubi:8.3 echo "Action is $ACT"
, the shell replaces $ACT with its value. As ACT=greet is a "podman thing", this doesn't set the variable in the shell where podman executes, so the ACT variable is unset, and the shell actually executes:
$ podman run -e ACT=greet ubi8/ubi:8.3 echo "Action is "
When echo is executed inside the container, the ACT variable is set, but nothing that is executed uses the variable.
So your printenv version of the command does work, because the variable is set.
A pattern you might see frequently is:
$ podman run -e ACT=greet ubi8/ubi:8.3 sh -c 'echo "Action is $ACT"'
The single quotes prevent variable expansion from happening, so when podman is executed, $ACT is not replaced by its value. Instead, the container runs the sh -c command. sh -c runs its argument in a shell, so it runs:
$ echo "Action is $ACT"
, where the variable is set, with the behavior you expected.
Cheers,
Álex

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 3,910 Views
Thanks - missed the point that the $ACT is explained by local bash first.