Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to detect if the display is valid in a controlled way? For example if I set $DISPLAY=anydisplay or $DISPLAY=0.0, is there a way to check if anydisplay or 0.0 is a valid display which I have access to? I am writing a program that is intended to be launched from a bash script and the idea is to warn the user if their display is not valid and run the program in 'console' mode.

share|improve this question

xhost is a minimalistic way to probe display accessibility.

#!/usr/bin/env bash

xhost +si:localuser:$( whoami ) >&/dev/null && { 
    echo "GUI"
    myapp --gui
} || {
   echo "console"
   myapp --console
share|improve this answer
error: must be on local machine to add or remove hosts. – Erik Aronesty Jun 26 '15 at 17:41
what's the complaint, exactly? As written, the code works (that message indicates display is accessible, and exit code is success) and conceals the the spurious error message. – Oh My Goodness Feb 11 at 21:51

You could just make the script exit with an informative error message. If you try to launch a graphical operation and the display is not accessible, you will get an error message. So, just attempt to launch whatever it is that you are doing graphically and if you can't exit and tell the user to re-run the script with another argument. For example:

#!/usr/bin/env bash

xterm 2>/dev/null || 
(echo "The display $DISPLAY is not accessible."
echo "Please run the script again using the '--no-gui' optiion" && 
exit 1)

Just replace xterm with whatever function you use to launch your GUI.

share|improve this answer

You must log in to answer this question.

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