当前位置: 首页>>技术问答>>正文


什么是$DISPLAY环境变量?

, ,

问题描述

我是shell脚本的新手。我不明白$DISPLAY环境变量是什么。

我有Ubuntu 13.10,我使用/bin/bash shell。我有两台显示器。

问题:

  1. 命令echo $DISPLAY将在我的机器上打印:0.0(在两台显示器上)。这是什么意思?

  2. 在哪种情况下,$DISPLAY变量是空白还是NULL?

  3. 有没有关于此的文章或教程?

最佳解决方案

X窗口系统中的神奇词是DISPLAY。显示包括(简化):

  • 键盘,

  • 一个鼠标

  • 和一个屏幕。

显示由服务器程序管理,称为X服务器。服务器为连接到它的其他程序提供显示功能。

远程服务器通过DISPLAY环境变量的定义知道它必须重定向X网络流量的位置,该变量通常指向位于本地计算机上的X Display服务器。

显示环境变量的值为:

hostname:D.S

哪里:

hostname是运行X服务器的计算机的名称。省略的主机名表示localhost。

D是序列号(通常为0)。如果有多台显示器连接到一台计算机,则可以改变它。

S是屏幕编号。显示器实际上可以有多个屏幕。通常只有一个屏幕,但默认值为0。

值的示例

localhost:4
google.com:0
:0.0

hostname:D.S表示主机主机名显示D上的屏幕S;此显示器的X服务器正在侦听TCP端口6000 + D.

host/unix:D.S表示主机主机D的屏幕S;此显示的X服务器正在UNIX域套接字/tmp/.X11-unix/XD中侦听(因此只能从主机访问)。

:D.S等同于host /unix:D.S,其中host是本地主机名。

:0.0表示我们正在讨论连接到本地主机中第一个显示器的第一个屏幕

阅读更多hereherehere

从X(7)手册页:

From the user’s perspective, every X server has a display name of the form:

hostname:displaynumber.screennumber

This information is used by the application to determine how it should connect to the server and which screen it should use by default (on displays with multiple monitors):

hostname The hostname specifies the name of the machine to which the display is physically connected. If the hostname is not given, the most efficient way of communicating to a server on the same machine will be used. displaynumber The phrase “display” is usually used to refer to collection of monitors that share a common keyboard and pointer (mouse, tablet, etc.). Most workstations tend to only have one keyboard, and therefore, only one display. Larger, multi-user systems, however, frequently have several displays so that more than one person can be doing graphics work at once. To avoid confusion, each display on a machine is assigned a display number (beginning at 0) when the X server for that display is started. The display number must always be given in a display name. screennumber Some displays share a single keyboard and pointer among two or more monitors. Since each monitor has its own set of windows, each screen is assigned a screen number (beginning at 0) when the X server for that display is started. If the screen number is not given, screen 0 will be used.

次佳解决方案

现有答案未能解决更广泛的问题。

如果您没有使用图形环境(即您正在系统控制台上登录而没有窗口等;或者您是通过SSH或类似设备从text-only终端远程登录,例如从运行PuTTY的Windows计算机登录)则没有GUI参与其中,DISPLAY通常会被取消。您与计算机通信的唯一方法是命令行(如果您知道如何,可能有方法可以转入GUI会话)。

如果您使用图形界面登录控制台(在Ubuntu上,通常使用GDM欢迎程序)或使用图形终端(例如从运行eXceed或mobaX的Windows计算机或VNC客户端等远程桌面软件) DISPLAY变量由管理图形会话的程序设置,以向图形客户端指示要连接的I /O设备。

传统上,Ubuntu计算机上的GUI运行X.org,X11实现,尽管最近,Canonical引入了名为Mir的现代化替代品;甚至最近,我相信Mir will be abandoned in favor of another project with broadly similar goals called Wayland。这些替换旨在降低完整X11堆栈的复杂性,我们不会在这里进行 – 它们遵循相同的DISPLAY约定,这是我们在此讨论的所有内容。

在X11上,DISPLAY的主机部分可以是远程服务器,您可以使用Ubuntu计算机作为”graphical terminal”来访问该远程服务器上的文件和程序(在这种情况下,您的计算机是”server”,它提供键盘,鼠标,以及在远程…服务器上运行的”client”程序的一个或多个显示设备。更常见的是,X11(或Mir,或Wayland)服务器和客户端程序(桌面管理器和各种图形客户端,如Web浏览器,电子邮件客户端,日历程序等)都可以在您的计算机上运行。这由DISPLAY值的”server”部分指示,在后一种情况下通常为空(这意味着默认值为localhost)。

X11服务器可以运行一个或多个图形会话 – 例如,您的控制台登录和远程VNC会话可以同时运行。在这种情况下(如果它们由相同的X11服务器实例管理),您在X11术语中有多个”display”。实际上,一个会话(一个登录事件和从中生成的桌面实例)是X11中的一个显示。

一个这样的显示器可以具有一个或多个屏幕。传统上,这意味着一台显示器,尽管原始架构有一些不幸的特征,例如无法将窗口从一个屏幕移动到另一个屏幕。像Xinerama和Xrandr这样的Add-ons进一步混淆了这种情况,一个屏幕经常以各种方式连接多个显示器。

如果您使用过multiple-monitor系统,您可能已经发现可以通过各种方式安排监视器并最终得到一个矩形区域,监视器显示监视器的某些部分,而其他部分未分配给任何监视器。这是X11创建的”screen”,如果你有多个显示卡,你可以拥有多个这些屏幕,每个屏幕都分配给一个或多个显示器(理论上,没有显示器运行; Xvfb利用它来允许你在没有任何监视器的情况下运行X11,只需将GUI映射到内存区域,无论出于何种目的)。

第三种解决方案

echo $DISPLAY will print :0.0 on my machine(on Both monitors). What this means?

:0.0表示显示编号0和屏幕编号0

In which case $DISPLAY will be blank or NULL?

如果您的$ DISPLAY出现错误,这通常不会发生

Are there any articles or tutorials on this?

一些教程和资源可以在这里找到:

第四种方案

来自https://help.ubuntu.com/community/EnvironmentVariables

可变显示

值示例:

:0.0
localhost:10.0
terminal01:0.0

它的用途是什么?

This variable is used to indicate to graphical applications where to display the actual graphical user interface, the value consists of 3 parts: A host-name followed by a colon (:), a display number followed by a dot (.) and a screen number.

The host-name part can be used to have the graphical output sent to a remote machine over the network. It can be omitted when the output is meant for an X server running on the local machine. The display number allows selecting among multiple X servers running on the same machine (Ubuntu uses multiple X servers to enable multiple graphical desktop sessions).

Although the screen number is used to select among multiple physical screen that are managed by the same X server, it is rarely set to anything other than “0” nowadays. Manually setting the “DISPLAY” environment variable’s value is rarely needed nowadays since it can be automatically and intelligently adjusted by many applications such as “GDM” and “SSH” when needed.

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/1872.html,未经允许,请勿转载。