fuzzybear3965
fuzzybear3965

Reputation: 263

env on macOS doesn't apply environment variables

I'm a bit confused by the following:

 /usr/bin/env  V=hello echo $V somestring

outputs only

somestring

not hello somestring like I would expect from man env.

However, the tail of

/usr/bin/env V=hello printenv

is

_=/usr/bin/env
V=hello

Why would this be happening?

Upvotes: 0

Views: 125

Answers (1)

Barmar
Barmar

Reputation: 782444

env sets the environment that's inherited by the child process that executes the command. But you're expanding the variable in the original shell, and passing the result as an argument to env.

Try this:

/usr/bin/env V=hello bash -c 'echo $V somestring'

The single quotes prevent the variable from being expanded in the original shell. Then you run a new shell process that expands the variable itself.

Upvotes: 3

Related Questions