如何在.tmux.conf中使用Bash变量?UART_LOGFILE_NAME调用报错求助
问题原因与解决方法
问题原因
tmux加载.tmux.conf的时机是会话初始化阶段,此时还未启动bash等shell进程,.bashrc里定义的环境变量还没被加载到tmux的运行环境中。你在send-keys里用${UART_LOGFILE_NAME}时,tmux会尝试解析自身环境中的这个变量,而此时变量不存在,因此抛出"invalid environment variable"错误。
解决方法
方法1:让tmux启动前继承变量
将UART_LOGFILE_NAME的导出语句从.bashrc移到.profile或.bash_profile中(这类文件在登录shell启动时会执行,tmux启动时会继承登录shell的环境):
# 在~/.profile或~/.bash_profile中添加 export UART_LOGFILE_NAME="/path/to/your/uart.log"
保存后重启终端,或者执行source ~/.profile,再启动tmux即可正常解析变量。
方法2:让shell解析变量而非tmux
把send-keys的命令用单引号包裹,让tmux直接把命令原封不动传给shell,等shell启动后(此时.bashrc已加载)再解析变量,同时记得添加C-m模拟回车执行命令:
new-session -s serial -n console split-window -h -l 30% send-keys 'sudo minicom rasp.serial -C ${UART_LOGFILE_NAME}' C-m split-window -v -l 10%
方法3:在tmux配置中直接定义变量
在.tmux.conf里用set-environment命令全局设置变量,这样tmux初始化时就能读取到:
set-environment -g UART_LOGFILE_NAME "/path/to/your/uart.log" new-session -s serial -n console split-window -h -l 30% send-keys "sudo minicom rasp.serial -C ${UART_LOGFILE_NAME}" C-m split-window -v -l 10%
内容的提问来源于stack exchange,提问作者Radek Hlavinka
相关产品推荐
相关产品推荐

