从子shell设置一个变量 [英] Set a variable from a subshell
问题描述
我设置父shell变量,从子shell怎么办?
A = 3
(α= 4)
回声$一个
子shell的全部意义就在于它的不的影响呼叫会话。在bash子shell是一个子进程,其他炮弹不同,但即使如此,在子shell变量设置不影响调用者。根据定义。
你需要一个子shell?如果你只需要一个组,然后使用大括号:
A = 3
{α= 4;}
回声$一个
给 4
(是一个细心的空格)。另外,变量值写到标准输出,并捕捉到它的调用者:
A = 3
a = $(A = 4;回声$一)
回声$一个
避免使用反单引号``,他们pcated德$ P $,可以是难以阅读。
How do I set a variable in the parent shell, from a subshell?
a=3
(a=4)
echo $a
The whole point of a subshell is that it doesn't affect the calling session. In bash a subshell is a child process, other shells differ but even then a variable setting in a subshell does not affect the caller. By definition.
Do you need a subshell? If you just need a group then use braces:
a=3
{ a=4;}
echo $a
gives 4
(be careful of the spaces in that one). Alternatively, write the variable value to stdout and capture it in the caller:
a=3
a=$(a=4;echo $a)
echo $a
avoid using back-ticks ``, they are deprecated and can be difficult to read.
这篇关于从子shell设置一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!