3

I can do the following:

$ FOO="text"
$ echo $FOO
$ text

But how can I wrap it inside bash -c construct? I tried this but failed:

$ FOO="text"
$ bash -c 'echo $FOO'
$ # return nothing

The reason I ask this because I need to execute another 3rd party code that need to be wrapped inside bash -c

codeforester
  • 39,467
  • 16
  • 112
  • 140
littleworth
  • 4,781
  • 6
  • 42
  • 76

2 Answers2

6

Try

$ export FOO="text"
$ bash -c 'echo $FOO'

export command is used to export a variable or function to the environment of all the child processes running in the current shell.

Here's the source

The "bash" command starts a child process where its parent is your current bash session.

To define a variable in parent process and use it in child process, you have to export it.

Luc M
  • 16,630
  • 26
  • 74
  • 89
  • 2
    `FOO=text bash -c 'echo $FOO'` would work as well - in this case, the environment variable `FOO` is set just in the context of the process (`bash`) being created. – codeforester Nov 29 '17 at 05:14
-1

you can use bash -c 'FOO=test; echo \$FOO' or export FOO=test;bash -c 'echo $FOO'

ceilKs
  • 27
  • 3