brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: echo $XDG_SESSION_TYPE tells you whether you are on Wayland or X11

Linux
~/tips/tip-linux-xdg-session-type-wayland-x11

echo "$XDG_SESSION_TYPE"

Prints the type of the current login session, so scripts can pick the right tool.

Half the advice about Linux desktop tools starts with “it depends on whether you run Wayland or X11”, and the other half assumes you know. The quickest way to find out is one environment variable.

echo "$XDG_SESSION_TYPE"

The pam_systemd man page describes $XDG_SESSION_TYPE as the session type, and lists the possible values: unspecified, tty, x11, wayland, mir and web. On a graphical login you will normally see wayland or x11.

In a script, a case statement is the tidy way to branch:

case "$XDG_SESSION_TYPE" in
  wayland) echo "Wayland session" ;;
  x11)     echo "X11 session" ;;
  *)       echo "something else: ${XDG_SESSION_TYPE:-unset}" ;;
esac

The :-unset part matters. In a minimal Ubuntu 24.04 container without a login session the variable was not set at all, and echo printed an empty line. A script that treats an empty value as a real answer will take the wrong branch.

That is also the main use. Tools for the clipboard, screenshots and window control differ between the two display systems, and a one-line check keeps one script working on both. I’d test this variable before reaching for a tool that only talks to X11.

Caveats: the man page says the variable is read by the module, and that the module sets it if it was not passed in and can be determined otherwise. The page doesn’t say how it is determined, so run the check in the desktop session itself.

Further reading: pam_systemd(8) man page

More Linux tips notify-send -r "$id" "Backup" "Done" pgrep -af 'sleep 301' smbstatus -L msmtp --serverinfo --tls --tls-certcheck=off --host=smtp.example.com --port=587 smbclient //server/share -U user -Tc backup.tar "*" All Linux tips (283) All topics

Link