When you're the root user in a shell, what is the command to obtain your regular username? I'm working scripts that need to obtain the regular username but must be executed as root.
I've found that "echo $USERNAME" works initially. However, I find that after I run the script, "echo $USERNAME" no longer works. It's as if the USERNAME environmental variable had been deleted.
Is there a better way?
Obtaining the username in a root script
Forum rules
LMDE 2 has reached end of support as of 1-1-2019
LMDE 2 has reached end of support as of 1-1-2019
Obtaining the username in a root script
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Obtaining the username in a root script
Work-a-round, if it works initially, then set your own variable from the info held in $USERNAME sort of:
Code: Select all
myuser=$USERNAME
Re: Obtaining the username in a root script
This will not work using "su -", as tested by myselfxircon wrote:Work-a-round, if it works initially, then set your own variable from the info held in $USERNAME sort of:Code: Select all
myuser=$USERNAME
Code: Select all
[anakin@stormbird ~]$ echo $USERNAME
anakin
[anakin@stormbird ~]$ su -
Password:
[root@stormbird ~]# echo $USERNAME
[root@stormbird ~]# logout
[anakin@stormbird ~]$ su
Password:
[root@stormbird anakin]# echo $USERNAME
anakin
[root@stormbird anakin]# exit
[anakin@stormbird ~]$ readonly USERNAME
[anakin@stormbird ~]$ export USERNAME
[anakin@stormbird ~]$ su -
Password:
[root@stormbird ~]# echo $USERNAME
[root@stormbird ~]# logout
[anakin@stormbird ~]$ export myname=$USERNAME
[anakin@stormbird ~]$ su -
Password:
[root@stormbird ~]# echo $myname
[root@stormbird ~]# logout
[anakin@schuldiner ~]$ sudo echo $USERNAME
[sudo] password for anakin:
anakin
[anakin@schuldiner ~]$
There is a command "logname", but that doesn't work on Mint, not by default anyway.Any shell invoked through sudo or su is a new shell. What su - does is to invoke a login shell, so that environment variables are reset and ~/.profile or ~/.bash_profile is read.
You could circumvent this by echoing $USERNAME to a file, then read that file while being root, but there's no proper solution I know about.
Regards,
Anakin
Re: Obtaining the username in a root script
Annakin - but it does work in straight su (no -):
Can't see a reason to assume the user environment of the target user, so a straight su should suffice.
Code: Select all
molly2@n5010 ~ $ myuser=$USERNAME
molly2@n5010 ~ $ echo $myuser
molly2
molly2@n5010 ~ $ su
Password:
n5010 molly2 # echo $myuser
n5010 molly2 # myuser=$USERNAME
n5010 molly2 # echo $myuser
molly2
n5010 molly2 #
Re: Obtaining the username in a root script
Yes, I tested that in my example as well. But in that situation you could also just use $USERNAMExircon wrote:Annakin - but it does work in straight su (no -):Code: Select all
molly2@n5010 ~ $ myuser=$USERNAME molly2@n5010 ~ $ echo $myuser molly2 molly2@n5010 ~ $ su Password: n5010 molly2 # echo $myuser n5010 molly2 # myuser=$USERNAME n5010 molly2 # echo $myuser molly2 n5010 molly2 #
Re: Obtaining the username in a root script
Without the OP's script, it has hard to see what is going wrong though