I'm trying to develop a small system app that is run as root.
In this app, I would like to list all environment variables of the default user (not the
$USER, but the user who calls runs the app as root, i.e.
$SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.
What I'm trying to accomplish is equivalent to:
- Change from root to user
- source ~/.bashrc or ~/.profile etc.
- printenv [and get it after the script]
- Change to root again
There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.
I need a one-liner for the aforementioned task. What I have right now is given below (run as root):
sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'
Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc
Any guidance is appreciated,
In case of missing information, please let me know.