Log in

No account? Create an account
Linux Community's Journal
[Most Recent Entries] [Calendar View] [Friends View]

Thursday, August 8th, 2013

Time Event
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

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

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


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'

<< Previous Day 2013/08/08
Next Day >>
About LiveJournal.com