Agnus Dei (jackal) wrote in linux,
Agnus Dei
jackal
linux

Linux Challenge Question of the Day

Very often I'll set a environment variable before running a command.

For example,
$ sudo JAVA_HOME=/usr/java/default /usr/local/activemq/default/bin/activemq-admin query -QQueue="my.activemq.queue.whatever"

But I've noticed that if I set a variable and then call echo to show that variable echo returns nothing.
$ FOO=bar echo $FOO

(nothing was returned)

But I can export the variable and then call echo and it works:
$ export FOO=bar ; echo $FOO
bar


Or if I set the varaible and then call "env" to check it I see the environment variable is set:
$ LALA=FUN env |grep LALA
LALA=FUN


So why can't I set the variable and then check it with echo like in the first example above?

Update:

I got my answer on facebook from Douglas Kilpatrick who wrote: because variable substitution is done before command parsing. So when the command gets run, it's already "FOO=bar echo ''"

Solution (provided by Douglas Kilpatrick):
$ FOO=baz eval 'echo $FOO'
baz
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 4 comments