how to export name of logged in user correctly? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of how to export name of logged in user correctly? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have the following script here.

#!/bin/bash
#some initial variables
DLURL=https://raw.github.com/horvan/drubuntu/master
DIRURL=/opt/.drubuntu
#Get files to run the installation
wget -O $DIRURL/install.sh $DLURL/scripts/install/install.sh > /dev/null 2>&1
wget -O $DIRURL/functions.sh $DLURL/scripts/install/functions.sh > /dev/null 2>&1
wget -O $DIRURL/filetemplates.sh $DLURL/scripts/install/filetemplates.sh > /dev/null 2>&1
export nameofuser=`echo $USER`
.install.sh
sudo bash "$DIRURL"/install.sh 2> errors.txt

after execution the value of $nameofuser is not something like username but root.

how to make sure to match the name of the logged in user?

Answer

Use

export nameofuser=`who am i | awk '{print $1}'`

 OR 

export nameofuser=`logname`

as $USER can change if you call the script using sudo, etc.

https://stackoverflow.com/questions/4598001/how-do-you-find-the-original-user-through-multiple-sudo-and-su-commands

We are here to answer your question about how to export name of logged in user correctly? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji