Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a variable inside bash -c command [duplicate]

Tags:

linux

bash

unix

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

like image 748
scamander Avatar asked Dec 20 '25 15:12

scamander


1 Answers

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.

like image 157
Luc M Avatar answered Dec 22 '25 05:12

Luc M



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!