21

This question already has an answer here:

What is DISPLAY=:0 and what does it mean?

It isn't a command, is it? (gnome-panel is a command.)

DISPLAY=:0 gnome-panel

marked as duplicate by Scott, Anthon, Jakuje, Wildcard, MelBurslan Apr 18 '16 at 18:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This question is not a duplicate. The linked question is explaining the format and meaning of the DISPLAY env variable whereas this question is asking about "assignment to an env variable and the affected program be written in the same line". – Haozhun Sep 10 '17 at 3:55
18

DISPLAY=:0 gnome-panel is a shell command that runs the external command gnome-panel with the environment variable DISPLAY set to :0. The shell syntax VARIABLE=VALUE COMMAND sets the environment variable VARIABLE for the duration of the specified command only. It is roughly equivalent to (export VARIABLE=VALUE; exec COMMAND).

The environment variable DISPLAY tells GUI programs how to communicate with the GUI. A Unix system can run multiple X servers, i.e. multiple display. These displays can be physical displays (one or more monitor), or remote displays (forwarded over the network, e.g. over SSH), or virtual displays such as Xvfb, etc. The basic syntax to specify displays is HOST:NUMBER; if you omit the HOST part, the display is a local one.

Displays are numbered from 0, so :0 is the first local display that was started. On typical setups, this is what is displayed on the computer's monitor(s).

Like all environment variables, DISPLAY is inherited from parent process to child process. For example, when you log into a GUI session, the login manager or session starter sets DISPLAY appropriately, and the variable is inherited by all the programs in the session. When you open an SSH connection with X forwarding, SSH sets the DISPLAY environment variable to the forwarded connection, so that the programs that you run on the remote machine are displayed on the local machine. If there is no forwarded X connection (either because SSH is configured not to do it, or because there is no local X server), SSH doesn't set DISPLAY.

Setting DISPLAY explicitly causes the program to be displayed in a place where it normally wouldn't be. For example, running DISPLAY=:0 gnome-panel in an SSH connection starts a Gnome panel on the remote machine's local display (assuming that there is one and that the user is authorized to access it). Explicitly setting DISPLAY=:0 is usually a way to access a machine's local display from outside the local session, such as over a remote access or from a cron job.

29

It's an environment variable that is passed just to that program, rather than the shell as a whole. This happens when you set a variable on the same line as a command.

X11 programs need to know where to display windows, since it's a client/server system and you could be displaying on a remote machine. This simply means use the first display on the local machine.

This is normally set up automatically when logging in to a desktop environment. For example, open a graphical terminal and type echo $DISPLAY.

  • does bash syntax allow the assignment to an env variable and the affected program be written in the same line, and separated by white space? – Tim Apr 1 '15 at 13:35
  • Yes, it's exactly as you quoted in the question. – teppic Apr 1 '15 at 13:36
  • 3
    @Tim All POSIX-compliant shells allow that. It means to set and export the environment variable just for that one command, but not to affect the value of the shell variable (if it already has a value) afterwards. – Mark Plotnick Apr 1 '15 at 15:00
  • 5
    @MarkPlotnick - it's a little more finer-grained than that. When prefixing a variable declaration to any command but a shell function or a special builtin the declaration should not affect the parent shell's definition for the spec'd var, but when doing it for either of those POSIX states that the variable declaration should stick. So, POSIXLY, one=$1 shift should simultaneously define the current shell variable $one to value the same as the current shell's first positional while removing said positional. bash, by the way, breaks w/ the spec by default. – mikeserv Apr 1 '15 at 15:15
  • 1
    @mikeserv Thanks as always for the more precise and correct description. – Mark Plotnick Apr 1 '15 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.