bash 如何为 linux shell 设置 TERM 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27188840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:54:17  来源:igfitidea点击:

How to set TERM environment variable for linux shell

linuxbashxterm

提问by David Crayford

I've got very odd problem when I set export TERM=xterm-256color in ~/.bash_profile. When I try to run nano or emacs I get the following errors.

当我在 ~/.bash_profile 中设置 export TERM=xterm-256color 时,我遇到了非常奇怪的问题。当我尝试运行 nano 或 emacs 时,出现以下错误。

nano:

纳米:

.rror opening terminal: xterm-256color

emacs:

电子邮件:

 is not defined.type xterm-256color
If that is not the actual type of terminal you have,
use the Bourne shell command `TERM=... export TERM' (C-shell:
`setenv TERM ...') to specify the correct type.  It may be necessary
to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.

If I manually enter the following into the shell it works

如果我手动将以下内容输入到 shell 中,它会起作用

export TERM=xterm-256color

I'm stumped.

我难住了。

回答by tripleee

Looks like you have DOS line feeds in your .bash_profile. Don't edit files on Windows, and/or use a proper tool to copy them to your Linux system.

看起来您的.bash_profile. 不要在 Windows 上编辑文件,和/或使用适当的工具将它们复制到 Linux 系统。

Better yet, get rid of Windows.

更好的是,摆脱Windows。

In more detail, you probably can't see it, but the erroneous line actually reads

更详细地说,你可能看不到它,但错误的行实际上是

export TERM=xterm-256color^M

where ^Mis a literal DOS carriage return.

哪里^M是文字 DOS 回车。

Like @EtanReisner mentions in a comment, you should not be hard-coding this value in your login files, anyway. Linux tries very hard to set it to a sane value depending onthings like which terminal you are actually using and how you are connected. At most, you might want to override a particularvalue which the login process often chooses but which is not to your liking. Let's say you want to change to xterm-256coloriff the value is xterm:

就像@EtanReisner 在评论中提到的那样,无论如何你都不应该在登录文件中硬编码这个值。Linux 会非常努力地将其设置为合理的值,具体取决于您实际使用的终端以及您的连接方式。最多,您可能希望覆盖登录过程经常选择但不符合您喜好的特定值。假设您想更改为xterm-256coloriff 值是xterm

case $TERM in xterm) TERM=xterm-256color;; esac

This is not a programming question and yet an extremely common question on StackOverflow. Please google before asking.

这不是一个编程问题,而是 StackOverflow 上一个非常常见的问题。提问前请先谷歌。