3

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?

janos
  • 120,954
  • 29
  • 226
  • 236
Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
  • What's the purpose of this anyway? The purpose of `export` is to make the variable available to subprocesses but `echo` is a shell built-in so there are no subprocesses of this shell. And obviously, the child shell you run cannot affect its parent's variables directly. – tripleee Aug 09 '17 at 04:21

1 Answers1

5

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'
janos
  • 120,954
  • 29
  • 226
  • 236