$TERM介绍

在开发Webshell时会遇到一些脚本中关于有$TERM环境变量的使用。没明白到底干嘛的。

这里总结下

https://static.1991421.cn/2024/2024-02-25-225930.jpeg

TERM

The $TERM is an environmental variable in Linux and Unix shell environments. This variable defines the terminal type. In other words, it sets the terminal type for which output is to be prepared.

如上可看出TERM定义了终端类型,不同类型会影响终端功能,比如颜色显示/光标控制

可能的值

进行SSH连接会话后,执行echo $TERM会看到值,可能是下面的某个

  • xterm

  • xterm-color

  • xterm-256color

  • vt220

  • vt100

  • dumb

  • linux

我个人实际测试,比如开启交互式会话,会看到是xterm-256color,当我走非交互会话执行命令会发现是dumb或空。通过上面理解term的含义也就理解非交互式会话功能简单,比如颜色根本不需要。

值设定

  1. 比如bashrc/bash_profile中都可以设置
  2. 客户端开启SSH会话时会进行设定。比如ssh2客户端。

修改测试

ubuntu系统下当我把bashrc中设置term为vt100或者xterm-256color,返回的终端信息,xterm-256color带颜色,vt100不带。

写在最后

如上也就了解了TERM,实际使用中该值并不需要自己进行设定,本身终端模拟器已经会进行设定。当然如果还是想自己设定也没问题。

相关文档