如何将shell变量导出到所有会话?

编程入门 行业动态 更新时间:2024-10-26 10:27:28
本文介绍了如何将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变量导出到所有会话?

本文发布于:2023-11-24 09:18:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何将   导出到   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!