linux - How to find out the user of parent shell inside a child shell? -
suppose user of parent shell foo
. if run sudo -u bar -i
, in child shell user bar
. so, how can find out user of parent shell inside child shell? namely, in case above, how can know user of parent shell foo
?
update
understand that, hack, possible achieve using ps
. however, more interested in less hacky , more standard way, if possible.
update 2
answers, resorted following solution:
# parent shell, user foo $ sudo -u bar -i parent=foo # child shell, show parent user foo $ ps u -p $ppid | grep parent | cut -d= -f2
you can use $ppid
variable assist along command or two:
#!/bin/bash user=`ps u -p $ppid | awk '{print $1}'|tail -1` echo $user
Comments
Post a Comment