Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
`
BAR

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
`
BAR

Any idea what is going on?

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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