这个问题已经在这里有了答案: > Difference between single and double quotes in Bash 6个
> Defining a variable with or without export 14个
我可以执行以下操作:
$FOO="text"
$echo $FOO
$text
但是如何将其包装在bash -c构造中?我尝试了这个但是失败了:
$FOO="text"
$bash -c 'echo $FOO'
$# return nothing
我之所以这样问,是因为我需要执行另一个需要包装在bash -c中的第三方代码
解决方法:
尝试
$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.
“ bash”命令启动一个子进程,其父进程是您当前的bash会话.
要在父进程中定义变量并在子进程中使用它,必须将其导出.