126

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:

  1. Command echo $DISPLAY will print :0.0 on my machine (on both monitors). What does this mean?

  2. In which cases will the $DISPLAY variable be blank or NULL?

  3. Are there any articles or tutorials on this?

2

5 Answers 5

151

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.

7
  • 1
    When this $DISPLAY will be null or BLANK. Mar 11, 2014 at 7:16
  • In normal This must not happen unless there is a problem in your display.. Mostly a restart of your display manager will solve the problem. But this is not a default action.
    – Maythux
    Mar 11, 2014 at 8:42
  • askubuntu.com/questions/432610/…
    – Maythux
    Mar 11, 2014 at 12:10
  • @ChandrayyaGK For example, when I'm not using any desktop environment like GNOME or KDE, but directly log in with TTY (like /dev/tty1), I got the blank $DISPLAY. Apr 18, 2017 at 20:01
  • 3
    @Maythux Sometimes the screen number seems to be omitted when being 0. My GNOME gives :0 for $DISPLAY. Apr 18, 2017 at 20:02
42

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).

4
  • Mir has indeed been abandoned along with Unity. For a long time lightdm rather than gdm was the default, but maybe that part of your answer has become correct again as it was before Unity's ascent! I use MATE, so I can't check trivially...
    – Zanna
    Jan 12, 2018 at 8:48
  • 1
    I was running Linux from console (without windows) and that explains everything. Makes actually sense :p Aug 13, 2019 at 7:12
  • Do you know how and where the DISPLAY variable is set? Because I know this is not present in any file. I suppose is inherited by every process. For example, the process /usr/bin/plasmashell has this variable set and all its child process (such as /usr/bin/plasma-systemmonitor,/usr/bin/ksysguardd) will have this same variable set. Jan 6, 2023 at 23:53
  • 1
    The X11 server (or well, uh, server client? The display manager, I would guess) sets it for you when it creates a session.
    – tripleee
    Jan 7, 2023 at 11:08
5

From https://help.ubuntu.com/community/EnvironmentVariables:

Variable- DISPLAY

Values Example:

:0.0
localhost:10.0
terminal01:0.0

What it's for?

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.

5

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

:0.0 means display number 0 and screen number 0

In which case $DISPLAY will be blank or NULL?

In case of error in your $DISPLAY and this doesn't happen normally

Are there any articles or tutorials on this?

Some tutorials and resource can be found here:

3

Recently I have been doing some scripting for some automatic launching of Firefox at a specific time for reminding me to clock in and out since I have been working from home. I found out that the DISPLAY variable is determined by the Display Manager, i.e. GDM3, LightDM, etc. I also made the discovery that the :0 or :1 is determined by AutomaticLogin. In GDM3 if I have the AutomaticLogin disabled, then my DISPLAY variable is :1. If I have AutomaticLogin enabled, DISPLAY is now :0. These numbers do not change regardless of number of monitors I have connected to my system.

The reason why this is important is that I use cron to launch a script for me at certain times to launch Firefox. cron does not have the variables set, in fact they are very limited, so at the top of my script after my SheBang line I have this line to export my DISPLAY type. Without exporting the DISPLAY in the script that is being called by crontab, Firefox will not launch and instead will give an error since no DISPLAY is set.

#This line checks if automatic login is disabled in gdm3 and sets DISPLAY.
grep "# AutomaticLogin" /etc/gdm3/custom.conf >/dev/null && export DISPLAY=":1" || export DISPLAY=":0"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .