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?


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'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.