Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I experienced something weird when working on a issue. This command does not seem to work. (Using ` to fill blank line).

When using export

> /bin/bash -c "echo $FOO; export FOO=BAR; echo $FOO"

This command however works as expected.

> echo $FOO; FOO=BAR; export FOO=BAR; echo $FOO

Same issue when setting with =

> /bin/bash -c "echo $FOO; FOO=BAR; echo $FOO"

This command however wokrs as expected.

> echo $FOO; FOO=BAR; FOO=BAR; echo $FOO

Any idea what is going on?

share|improve this question
up vote 2 down vote accepted

It's a problem with the double quotes. Your shell (the 'external' one) will expand $FOO before runnig the command. If you set $FOO to something (let's say "FOO") you actually run this command.

/bin/bash -c "echo FOO; export FOO=BAR; echo FOO"

And if you don't set FOO, you run this commmand

/bin/bash -c "echo ; export FOO=BAR; echo "

Please use single quotes to get what you expect.

/bin/bash -c 'echo $FOO; FOO=BAR; echo $FOO'
share|improve this answer
Thank you! This caused me a headache.. – Will Nov 7 '13 at 23:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .