cancel
Showing results for 
Search instead for 
Did you mean: 
ekimmbi
Mission Specialist
Mission Specialist
  • 2,922 Views

Why this command doesn't pass the environment variable?

Jump to solution

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?

 

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
alexcorcoles
Flight Engineer
Flight Engineer
  • 2,900 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

 

View solution in original post

4 Replies
Ravi_Shanker
Flight Engineer
Flight Engineer
  • 2,909 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.

Certification ID: 111-010-393
Marek_Czernek
Flight Engineer Flight Engineer
Flight Engineer
  • 2,860 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.

0 Kudos
alexcorcoles
Flight Engineer
Flight Engineer
  • 2,901 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

 

ekimmbi
Mission Specialist
Mission Specialist
  • 2,882 Views

Thanks - missed the point that the $ACT is explained by local bash first.

0 Kudos
Join the discussion
You must log in to join this conversation.