Score:0

How to get variable environment from parent user?

tr flag

I need to know how it's possible to get environment variable from a parent user after su? For example if I'm evan user and I have variable $DISPLAY=:10.0, then I use "su -" to go to root user, how could I get the $DISPLAY variable from parent evan user?

kanehekili avatar
zw flag
Looks like a XY [problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Might tell us, why you need the display in root...
antonio1 avatar
tr flag
very well. I need to su in root to run a script, but later in script I have to launch xclock which is not working because something like display problem is not inherited from original user. If I set manually the DISPLAY as original user evan, everything works nice, but the display value is changing.
Score:0
zw flag

In this case I'd look at xhost

Usually I could also manage to get some GUI open as root (depending on the linux distro) with defining

export DISPLAY=:0.0

in my script.

That the display id is changing as you have written is unknown to me.

antonio1 avatar
tr flag
as soon as the DISPLAY is different than original opened one DISPLAY=:10.0 it does not work in same session.
Score:0
tr flag

I found the solution by using following lines:

prev_user=$(pstree -lu -s $$ |grep --max-count=1 -o '([^)]*)' |head -n 1 | sed 's/[()]//g')
inherit_display=$(xauth -f /home/$prev_user/.Xauthority list|tail -1|cut -d/ -f2|cut -d ' ' -f1)

export DISPLAY="$inherit_display"

prev_user - is getting the previous user (parent user) from which I used su

inherit_display - get exactly the value of DISPLAY for parent user

finally I export the value to my new DISPLAY for root.

works like a sharm.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.