I'm using Arch Linux and things are not so easy when you try to discover the current Xorg DISPLAY (not through the env variable).
Here is a working example:
$ cat /sys/class/tty/tty0/active
tty2
who | awk -v term="tty2" '/\(:/ $0 ~ term { gsub(/\(|\)/, "", $0); print $5 }'
:0
That works in some cases, in others not:
$ cat /sys/class/tty/tty0/active
tty3
who | awk -v term="`cat /sys/class/tty/tty0/active`" '/\(:/ $0 ~ term { gsub(/\(|\)/, "", $0); print $5 }'
NOTHING
Cause:
$ who
uzumaki tty2 2015-05-16 10:50 (:0)
guest pts/2 2015-05-16 11:47 (:1)
uzumaki tty4 2015-05-16 11:07
The guest user (tty3) had his session lauched using pts (pseudo terminal).
I'm using gdm and it launches xorg servers without a specific command (not clarifying the display number).
$ ps au | grep -e Xorg -e vt
uzumaki 788 1.4 0.9 740464 150672 tty2 Sl+ 10:50 5:38 /usr/lib/xorg-server/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
guest 1062 0.0 0.1 465748 32344 tty3 Sl+ 10:51 0:19 /usr/lib/xorg-server/Xorg vt3 -displayfd 3 -auth /run/user/1002/gdm/Xauthority -nolisten tcp -background none -noreset -keeptty -verbose 3
So the million question is: How to find out the current (in use) XServer DISPLAY number? Current because, as above, we can have multiples servers (multiple users).