bash 确定终端类型(经典 Unix 终端 vs 图形终端)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17814123/
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
Determining the type of terminal (classic Unix terminal vs graphical terminal)
提问by prcastro
I'm configuring my prompt (PS1
) via .bashrcand found one issue with my current configuration: I am using a 256 color scheme. This is not compatible with the classical terminal (accessible via e.g. Ctrl+Alt+F2) but looks beautiful in graphical terminals such as gnome-terminal, terminator, etc.
我正在PS1
通过.bashrc配置我的提示 ( )并发现我当前的配置存在一个问题:我使用的是 256 配色方案。这不是与经典的终端兼容(可通过例如Ctrl+ Alt+ F2),但看起来美丽在图形终端,诸如侏儒末端,终止子等。
So I have to change my prompt depending on the type of terminal. To do this, I need a condition for if
clause to test the type of terminal. Do you know how to do this?
所以我必须根据终端的类型更改我的提示。为此,我需要一个条件 forif
子句来测试终端的类型。你知道怎么做吗?
回答by uml?ute
the TERM
variable indicates the terminal type.
when running in an x-terminal, it is usually xterm
(but can also be xterm-color-256
as Dmitry has hinted in his answer).
the following code checks whether the value of $TERM starts with xterm
(and thus catches several cases):
该TERM
变量指示的终端类型。在 x 终端中运行时,通常是xterm
(但也可以xterm-color-256
像 Dmitry 在他的回答中暗示的那样)。以下代码检查 $TERM 的值是否以xterm
(并因此捕获了几种情况)开头:
case "$TERM" in
xterm*)
echo "running as an x-terminal"
;;
*)
echo "not running as an x-terminal"
;;
esac
回答by Varun
echo $TERM
would give you the terminal type
echo $TERM
会给你终端类型
回答by glenn Hymanman
Another approach: look at the parent process of the current shell. If its "login", you're in a console
另一种方法:查看当前shell的父进程。如果是“登录”,则您在控制台中
parent=$(ps --pid $(ps --pid $$ --no-headers --format ppid) --no-headers --format cmd)
if [[ $parent == login* ]]; then
echo console
PS1='plain> '
else
echo assume you can get away with more
PS1='fancy> '
fi
回答by Dmitry
This should work:
这应该有效:
if [ "$TERM" == "xterm-color-256" ]; then echo "YES"; fi
回答by David W.
You could use the value of $TERM
to decide if you have a color terminal or not, but this value could be modified. The question is where this environment variable is being set when a new terminal window is opened.
您可以使用 的值$TERM
来决定是否有彩色终端,但可以修改此值。问题是当打开一个新的终端窗口时,这个环境变量在哪里设置。
This would be in the .bashrc
file. However, a word of warning:
这将在.bashrc
文件中。但是,警告一句:
- The value of
$TERM
may be a lie. This is just an environment variable that's set. How it is set is determined by the terminal program (on the Mac, theTerminal.app
can set the terminal toxterm
,xterm-color
,vt100
,ansi
, and several others.. - The terminal could be a color terminal, but doesn't use ANSI color codes. You could be in trouble if you simply assume that a particular escape sequence gets you a particular color.
- If your prompt is set in the
.bashrc
file, changing the value of$TERM
won't change the prompt.
- 的价值
$TERM
可能是谎言。这只是一个设置的环境变量。它是如何集由终端程序(确定在Mac上,则Terminal.app
可以设置终端xterm
,xterm-color
,vt100
,ansi
,和其他几个人.. - 终端可以是彩色终端,但不使用 ANSI 颜色代码。如果您只是假设特定的转义序列会为您提供特定的颜色,那么您可能会遇到麻烦。
- 如果您在
.bashrc
文件中设置了提示,则更改 的值$TERM
不会更改提示。
That said, I would probably do something like this:
也就是说,我可能会做这样的事情:
case $TERM in
*color*) PS1=...;;
*) PS1=...;;
esac
This way, my terminal will be set to color if I said it was an xterm-color
or xterm-256color
.
这样,如果我说它是一个xterm-color
或,我的终端将被设置为彩色xterm-256color
。