What is the $DISPLAY environment variable?
I am new to shell scripting. I don't understand what the $DISPLAY
environmental variable is.
I have Ubuntu 13.10 and I use /bin/bash
shell. I have two monitors.
Questions:
Command
echo $DISPLAY
will print:0.0
on my machine (on both monitors). What does this mean?In which cases will the
$DISPLAY
variable be blank or NULL?Are there any articles or tutorials on this?
The magic word in the X window system is DISPLAY. A display consists (simplified) of:
- a keyboard,
- a mouse
- and a screen.
A display is managed by a server program, known as an X server. The server serves displaying capabilities to other programs that connect to it.
The remote server knows where it has to redirect the X network traffic via the definition of the DISPLAY environment variable which generally points to an X Display server located on your local computer.
The value of the display environment variable is:
hostname:D.S
where:
hostname is the name of the computer where the X server runs. An omitted hostname means the localhost.
D is a sequence number (usually 0). It can be varied if there are multiple displays connected to one computer.
S is the screen number. A display can actually have multiple screens. Usually, there's only one screen though where 0 is the default.
Example of values
localhost:4
google.com:0
:0.0
hostname:D.S
means screen S on display D of host hostname; the X server for this display is listening at TCP port 6000+D.
host/unix:D.S
means screen S on display D of host host; the X server for this display is listening at UNIX domain socket /tmp/.X11-unix/XD (so it's only reachable from host).
:D.S
is equivalent to host/unix:D.S, where host is the local hostname.
:0.0 means that we are talking about the first screen attached to your first display in your local host
Read more here: support.objectplanet.com and here: superuser.com and here: docstore.mik.ua.
From a X(7) man page:
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 a 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.
The existing answers fail to address the broader picture.
If you are not using a graphical environment (i.e. you are logging in on the system console with no windows etc; or you are logging in remotely from a text-only terminal over SSH or similar, such as from a Windows computer running PuTTY) then no GUI is involved, and DISPLAY
will typically be unset. Your only means of communicating with the computer is the command line (though there may be ways to pivot into a GUI session if you know how).
If you are logging in on the console with a graphical interface (on Ubuntu, typically the GDM greeter is used) or using a graphical terminal (such as from a Windows computer running eXceed or mobaX, or remote desktop software like a VNC client) the DISPLAY
variable is set up by the program which manages your graphical session to indicate to graphical clients which I/O devices to connect to.
Traditionally, the GUI on an Ubuntu computer was running X.org, an X11 implementation, though more recently, a modernized replacement called Mir was introduced by Canonical; and even more recently, I believe Mir will be abandoned in favor of another project with broadly similar goals called Wayland. These replacements are intended to reduce the complexity of a full X11 stack, which we will not be going into here -- they adhere to the same DISPLAY
convention, which is after all what we are discussing here.
On X11, the host part of DISPLAY
could be a remote server, and you would use your Ubuntu computer as a "graphical terminal" to access files and programs on that remote server (in which case your computer is the "server" which serves a keyboard, a mouse, and one or more display devices to "client" programs running on the remote ... server). More commonly, the X11 (or Mir, or Wayland) server and the client programs (a desktop manager and various graphical clients such as a web browser, an email client, a calendar program, etc) all run on your computer. This is indicated by the "server" part of the DISPLAY
value, which in the latter case is typically empty (which implies the default value, localhost
).
An X11 server may run one or more graphical sessions -- for example, your console login and a remote VNC session could be running at the same time. In this case (if they are managed by the same X11 server instance) you have more than one "display" in X11 terms. In practice, one session (one login event and the desktop instance spawned from this) is one display in X11.
One such display can have one or more screens. Traditionally, this meant one monitor, though the original architecture had some unfortunate traits such as the inability to move a window from one screen to another. Add-ons like Xinerama and Xrandr further muddied the situation to the point where one screen often connects multiple monitors in various ways.
If you have played with multiple-monitor systems, you have probably discovered that you can arrange monitors in various ways and end up with a rectangular area where your monitors display some parts of it and other parts are not assigned to any monitor. This is the "screen" that X11 creates, and if you have more than one display card, you can have multiple of these screens, each assigned to one or more monitors (or in theory, running without a monitor; Xvfb exploits this to allow you to run X11 without any monitors, simply mapping the GUI to a memory region for whatever purpose).