如何将shell变量导出到所有会话? [英] How to export a shell variable to all sessions?

查看:58
本文介绍了如何将shell变量导出到所有会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以将我的shell变量导出到系统中的所有会话(不仅是当前会话).我不希望在.bashrc文件中设置它,因为shell变量是动态变量,它会不时更改.

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.

推荐答案

通过在 .bashrc 中的DEBUG上设置陷阱,可以设置会话以继续重新读取磁盘上的文件:

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

如果将终端A保持打开状态,请运行 echo VAR = 42>>〜/.myvars 在终端B中,然后切换回终端A并回显$ VAR ,它将被神奇地"设置.

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.

这篇关于如何将shell变量导出到所有会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆