2

In bash, I can pass an environment variable to a single command in the following way:

KEY=VAL <command>

However, I don't understand why the following doesn't work:

KEY=VAL echo $KEY

While this works:

KEY=VAL bash -c 'echo $KEY'

i.e. the first one prints a blank line while the other prints "VAL". I'd expect both to print "VAL".

Wahaj Ali
  • 4,093
  • 3
  • 23
  • 35

1 Answers1

2

Because KEY=VAL echo $KEY isn't having echo expand the $KEY variable.

The current shell is doing that before it runs echo (or whatever).

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148