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