Home | News | Download | Packages | Forum | Wiki | Github

SOLVED: Way to check running graphical environment


What would be a good way from within a bash script to detect whether a graphical environment is accessible to the executing user?

I had been recommended xterm -iconic -e echo but since xterm is not installed by default, I would prefer a more ‘native’ way.


You can check the contents of $DISPLAY; if it’s empty then the shell is not running under X.


I can’t see much wrong with that, very simple…!


…but the inverse is not always true; having non-empty $DISPLAY doesn’t mean that X server is available. If that’s important to you then it’s better to use something like xhost or xset q:

xhost > /dev/null 2>1 || echo "no X :("


Thanks for this. If $DISPLAY is set but no X running I’d get in trouble, and will need an extra check, so better to check with xhost; I think just xhost &>/dev/null which is also false when isn’t in PATH (I’m going to assume no X is present).

Turned out that neither xhost nor xset was present on my install of Void, so I am using xrandr to the same effect.