It should be possible to automatically discover the first active display in use by the given user using the following logic:
if w | grep -P '^(naftuli)' | awk '{print $2;}' | grep -qP '^tty\d' ; then
# we have naftuli logged in at a physical TTY on the machine, not guaranteed to be X
if w | grep -P '^(naftuli)' | grep -P '\b(tty\d)\b' | awk '{print $3;}' | grep -qP '^:\d+' ; then
# we have naftuli logged in at a physical TTY with what looks like an X display
DISPLAY="$(w | grep -P '^(naftuli)' | grep -P '\b(tty\d)\b' | awk '{print $3;}')"
fi
fi
It should be possible to automatically discover the first active display in use by the given user using the following logic:
Special props to Gilles for his answer to this X question on unix.stackexchange.