Re: shell script: test for graphical session
On 2019-10-29 14:32:39 +0900, John Crawley wrote:
> A (bash) script wants to put up a GUI (yad) if it can, or otherwise try to
> interact with the user on the terminal.
>
> Previously I was using loginctl:
> loginctl show-session -p Type $XDG_SESSION_ID
> and looking for 'Type=x11' or 'Type=wayland'
>
> However, if a user logs in on a tty and then runs 'startx' (I don't know if
> Wayland has an equivalent) then the above command returns 'Type=tty' even
> when X is running.
> ($XDG_SESSION_TYPE gives the same results.)
One also gets Type=tty after SSH with X forwarding (ssh -X).
> I've replaced the test with
> [[ -n $DISPLAY || -n $WAYLAND_DISPLAY ]] && <use GUI>
> but environment variables are a bit fragile and I was wondering if there
> might be a better way.
Actually they seem better as you can switch the X11 display
(or disable it) by changing the value of $DISPLAY, and the
GUI will probably honor this choice.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
Reply to: