bash forgets export variable

青春壹個敷衍的年華 提交于 2020-12-06 07:21:40

问题


I am unable to make even the simplest export of variables, from within scripts, to work in my bash - what am I dooing wrong?

File test.sh :

#!/bin/bash
echo $ttt
ttt="fffalse"
export ttt
echo $ttt

bash test :

hpek@hpek:~/temp$ export ttt="tttrue"
hpek@hpek:~/temp$ ./test.sh 
tttrue
fffalse
hpek@hpek:~/temp$ ./test.sh 
tttrue
fffalse
hpek@hpek:~/temp$ 

Edit:

I now know from the answers, that this will not work. -but how can make a single variable remembered between processes? Do I need to store it in a file?


回答1:


./test.sh is the same as bash test.sh

Each shell script running is, in effect, a subprocess (child process) of the parent shell.
And subprocess cannot export env-var to it's parent.


You can try this(run in the same environment):

. test.sh



回答2:


export works in the current process and any children spawned afterward; it does not work across process boundaries (parents, existing children, unrelated processes). The environment behaves like a sort of shadow argument list, not like a filesystem or mailbox.



来源:https://stackoverflow.com/questions/10457725/bash-forgets-export-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!