首页 > 解决方案 > tmux 从 tmux 配置中的 shell 获取变量值

问题描述

有什么方法可以通过 shell 获取变量到 tmux config 的值。

我写了一些脚本,但它不能设置为像 bash 脚本这样的变量。

enName=$(ip addr show | awk '/inet.*brd/{print $NF}' | grep -o -P '^e.*')
wlName=$(ip addr show | awk '/inet.*brd/{print $NF}' | grep -o -P '^w.*')
[[ ! -z $wlName ]] && interface=$wlName || interface=$enName
ip=$(ifconfig $interface | grep 'inet ' | awk '{print $2}')
net_device_ip="$interface $ip"

- 编辑 -

实际上我想要的是在 tmux 配置文件中维护变量 net_device_ip 。如果可能,bash 脚本在其配置中以 tmux 方式运行。

我想设置的就像: set -g status-left " #[fg=colour160]#S #{net_device_ip}"

如何通过使用 tmux 命令 run-shell & set-environment 设置变量和运行 shell

谢谢

(tmux 版本:3.0a)

标签: bashconfigurationtmux

解决方案


你几乎拥有它。您只需要 a$而不是 a#对您的变量评估。

我在我的.tmux.conf.

我有点不清楚你是否试图将你的脚本和你的 tmux conf 文件作为两个不同的部分来维护,但你需要从你的文件中运行脚本或将脚本写入你的.tmux.conf文件,以便net_device_ip在范围内定义.

因此,一个完整的解决方案是将其添加到.tmux.conf文件的末尾:

enName=$(ip addr show | awk '/inet.*brd/{print $NF}' | grep -o -P '^e.*')
wlName=$(ip addr show | awk '/inet.*brd/{print $NF}' | grep -o -P '^w.*')
[[ ! -z $wlName ]] && interface=$wlName || interface=$enName
ip=$(ifconfig $interface | grep 'inet ' | awk '{print $2}')
net_device_ip="$interface $ip"

set -g status-left " #[fg=colour160]#S ${net_device_ip}"

推荐阅读