Search code examples
bashexportquoting

export variable within `/bin/bash -c`


I'm trying to export variable within the /bin/bash -c command.

This results in empty output:

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

What would be the proper way to do that?


Solution

  • Since you double-quoted the command, the $FOO got evaluated in your current shell, not by the /bin/bash -c. That is, what actually got executed was this:

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

    Enclose in single-quotes:

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

    An equivalent shorter form:

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