I would like to know is there a way to export my shell variable to all sessions in the system (not only the current session). I'm not looking to set it in .bashrc file as the shell variable is a dynamic one it changes time to time.
问题:
回答1:
You can set up your sessions to keep rereading a file on disk by setting a trap on DEBUG in your .bashrc
:
trap 'source ~/.myvars' DEBUG
If you leave a terminal A open, run echo VAR=42 >> ~/.myvars
in terminal B, then switch back to terminal A and echo $VAR
, it'll "magically" be set.
回答2:
You seem to misunderstand what export
does. All it does is to move a local variable into the environment block within the process (/proc/$$/environ
).
When a new process is created (a fork
) then the program data areas, including the environment block, are copied to the new process (actually they are initially shared, then copied when one writes). When a different program is run (execve), by default the environment block remains from the previous program. See also the env(1)
program.
So environment variables are normally inherited (copied) from their parent process. The only way to get a new environmnt variable into a running process is to use some sort of inoculation technique, as a debugger would do. Writing such a program is not an easy task, and I'm sure you could imagine the security implications.
回答3:
You can't. A better explanation can be found in the unix stackexchange section here!
A shell variable probably is not suited for the use you are trying to achieve. Maybe you want to use files instead.