Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export variable within `/bin/bash -c` [duplicate]

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?

like image 729
Peter Butkovic Avatar asked Feb 14 '26 17:02

Peter Butkovic


1 Answers

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'
like image 200
janos Avatar answered Feb 16 '26 08:02

janos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!