问题
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