Is it possible to update a varible from one shell

2019-02-28 20:00发布

How can we update a variable from one shell to another shell ?

Suppose , I am having 2 Putty sessions opened , I want to set a variable in the first SHELL and I need that variable to access from the 2nd SHELL .

Is is possible ?

标签: linux bash shell
3条回答
迷人小祖宗
2楼-- · 2019-02-28 20:04

As each process' environment is protected, there's no way to share environment variables. I would suggest using a file on a shared filesystem to store the variable you want and reading that file in whenever you'd need to know what the new value is.

查看更多
倾城 Initia
3楼-- · 2019-02-28 20:25

You can save the variable to a script. Then source the script in the 2nd session.

For example:

# session 1
hello=world
echo "hello=$hello" > /tmp/var.sh

# session 2
. /tmp/var.sh
echo $hello
查看更多
SAY GOODBYE
4楼-- · 2019-02-28 20:27

It is usually not possible, because each shell (and each process) has its own environment. See execve(2).

However, you might want to switch to the fish shell. It gives you so called universal variables which might be shared between several instances of (i.e. processes running) the fish shell. This is implemented thru the fishd user daemon (with which every fish process communicates).

查看更多
登录 后发表回答