Export variable to `/ bin / bash -c`

I am trying to export a variable in a command /bin/bash -c

.

The result is empty output:

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

      

What would be the correct way to do this?

+3


source to share


1 answer


Since you are specifying the command twice, $FOO

got evaluated in your current shell, not /bin/bash -c

. That is, what was actually accomplished was the following:

/bin/bash -c 'export FOO=foo; echo '

      

Enclose in single quotes:



/bin/bash -c 'export FOO=foo; echo $FOO'

      

equivalent shorter form:

FOO=foo /bin/bash -c 'echo $FOO'

      

+3


source







All Articles