This question already has an answer here:
这个问题已经有了答案:
- Difference between single and double quotes in Bash 3 answers
- Bash 3中的单引号和双引号之间的差异
- Defining a variable with or without export 14 answers
- 定义一个变量或不导出14个答案。
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:
但是如何在bash -c构造中包装它呢?我试过了,但是失败了:
$ 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
之所以这样问,是因为我需要执行另一个需要在bash -c中包装的第三方代码
2 个解决方案
#1
4
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.
export命令用于将变量或函数导出到当前shell中运行的所有子进程的环境中。
这是源
The "bash" command starts a child process where its parent is your current bash session.
“bash”命令启动子进程,其父进程是当前的bash会话。
To define a variable in parent process and use it in child process, you have to export it.
要在父进程中定义一个变量并在子进程中使用它,您必须导出它。
#2
-1
you can use bash -c 'FOO=test; echo \$FOO'
or export FOO=test;bash -c 'echo $FOO'
您可以使用bash -c 'FOO=test;echo \$FOO'或导出FOO=test;bash -c 'echo $FOO'
#1
4
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.
export命令用于将变量或函数导出到当前shell中运行的所有子进程的环境中。
这是源
The "bash" command starts a child process where its parent is your current bash session.
“bash”命令启动子进程,其父进程是当前的bash会话。
To define a variable in parent process and use it in child process, you have to export it.
要在父进程中定义一个变量并在子进程中使用它,您必须导出它。
#2
-1
you can use bash -c 'FOO=test; echo \$FOO'
or export FOO=test;bash -c 'echo $FOO'
您可以使用bash -c 'FOO=test;echo \$FOO'或导出FOO=test;bash -c 'echo $FOO'