Shell – Get DISPLAY value by username

shell

I run following commands to get DISPLAY value for a logged in user.

$ loginctl list-sessions
   SESSION        UID USER             SEAT            
        c2       1000 asif             seat0               
        c3       1001 john             seat1

Then use one of SESSION value in following command

$ loginctl show-session -p Display -p Active c3
Display=:20
Active=yes

Here I get Display=:20. Isn't there any short way to get this value for any logged in user by username or uid?

I made following shell script as well to do the job, but it's getting really lengthy.

#!/bin/bash
clear
BASEDIR=$(dirname $0)
CUID=`id -u` 
SESSIONS=`loginctl list-sessions`
readarray -t SESSIONS <<< "$SESSIONS"
for LN in "${SESSIONS[@]}"
do
    W=($LN)
    SID="${W[0]}"
    USD="${W[1]}"
    USR="${W[2]}"
    SET="${W[3]}"
    if [ $CUID == $USD ]; then
        SINFO=(`loginctl show-session -p Display -p Active $SID`)
        DISPL="${SINFO[0]}" 
        DISP=( `echo $DISPL | cut -f2 -d=` )
        export DISPLAY=$DISP && /usr/bin/python $BASEDIR/run.pyw
    fi
done

I am using Linux Mint XFCE.

Best Answer

Here is a script that will show the display of specified user.

Let's name this script showuser:

#!/bin/bash
loginctl show-session $(sudo loginctl -a show-user $1 | grep Display | sed 's/Display.//g') | grep Display

Now, you can run the script followed by a user name and it will return the display.

Example:

~$ ./showuser john
Display=:20
Related Question