окружить переменную, не экспортируемую из подоболочки?

В то время как прохождение через учится удару на горьком опыте, я нашел это subshells наследовал переменные от их родителя, и они могут перезаписать его в своем собственном объеме. и это не будет отражено к родительской оболочке, если мы не будем использовать export,

Я попробовал ниже примера для экспорта VAR1, и я не получаю значение VAR1, отраженное к моей родительской оболочке. Может кто-то объяснять, Пропускаю ли я что-то здесь, Заранее спасибо.

anupam:~$ VAR1="variable under shell $$.pid and $BASHPID.bashid"
anupam:~$ set -o posix; set | grep -i VAR1
VAR1='variable under shell 6137.pid and 6137.bashid'
anupam:~$ (
> echo Inside the subshell
> echo ${VAR1}
> VAR1="variable under subshell with $$.pid and $BASHPID.bashid"
> echo ${VAR1}
> )
Inside the subshell
variable under shell 6137.pid and 6137.bashid
variable under subshell with 6137.pid and 6193.bashid
anupam:~$ set -o posix; set | grep -i VAR1
VAR1='variable under shell 6137.pid and 6137.bashid'
anupam:~$ (
> echo Inside another subshell
> echo ${VAR1}
> VAR1="variable under another subshell with $$.pid and $BASHPID.bashid"
> echo ${VAR1}
> export VAR1="exported variable under another subshell with $$.pid and $BASHPID.bashid"
> )
Inside another subshell
variable under shell 6137.pid and 6137.bashid
variable under another subshell with 6137.pid and 6208.bashid
anupam:~$ set -o posix; set | grep -i VAR1
VAR1='variable under shell 6137.pid and 6137.bashid'
anupam:~$ 
0
задан 17 October 2018 в 01:09

1 ответ

Ребенок не может изменить среду родителя. Можно экспортировать переменные от родителя до детей, не наоборот.

3
ответ дан 27 October 2019 в 07:53

Другие вопросы по тегам:

Похожие вопросы: