bashenvironment-variablesxserver

Why is $DISPLAY sometimes :0 and sometimes :1


I'm using xmacro to record keyboard shortcuts, which requires a $DISPLAY to replay on. But, sometimes my $DISPLAY is :0 and sometimes :1, so every time that happens I have to change the value manually. Why does it keep changing, and is there a way to set the $DISPLAY value to either :0 or :1 permanently? (I can export DISPLAY=:0 in one terminal, but that doesn't change the value of $DISPLAY in new terminals.)


Solution

  • The number identifies the display ("a collection of monitors that share a keyboard and mouse")

    :0 is usually the local display (i.e. the main display of the computer when you sit in front of it).

    :1 is often used by services like SSH when you enable display forwarding and log into a remote computer.

    It can also be modified by startup scripts which try to "fix" it. To find out whether this is happening, run

    grep DISPLAY ~/.??*
    

    .??* is a trick to get all dot files without .. and . (parent and current folder).

    If that doesn't print anything, check /etc/profile, /etc/bash* and /etc/bash*/* in a similar manner.

    I couldn't find a useful manual for xmacro but most X11 application support the option -d or -display to override $DISPLAY.

    If this doesn't work, create xmacro.sh with this content:

     #!/bin/bash
    
     export DISPLAY=:0
     exec xmacro "$@"
    

    Now invoke the tool with xmacro.sh and it should always work.