LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2009, 06:41 PM   #1
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
echo $USER/ sudo echo $USER


Could anybody explain why in a script run as sudo, echo $USER will return root, but sudo echo $USER will always return the username running sudo. For a more clear question:

Code:
jc@jcmain:~$ sudo echo $USER
jc
jc@jcmain:~$ sudo ./sudo_demo.sh 
root
jc@jcmain:~$ cat sudo_demo.sh
#!/usr/bin/env bash
echo $USER
I essentially need the code running as sudo to return 'jc', checking the wd is not viable here, either. Ideas?

Last edited by jamescondron; 06-03-2009 at 06:42 PM. Reason: missed a sudo
 
Old 06-03-2009, 06:53 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
well $USER is set by the shell.

I reckon...
the line is evaluated first by the shell and all the variables
interpolated, then the command is forked off to execute

so:

sudo echo $USER

runs as:

sudo echo jc

it happens before the sudo
 
Old 06-03-2009, 07:05 PM   #3
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Original Poster
Rep: Reputation: 70
That actually sounds reasonable put that way, I really hadn't thought it like that. Ah well, I may just script in an extra arg to handle this stuff instead, feels messier, but nevermind
 
Old 06-03-2009, 07:06 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
The difference is because you are telling sudo to apply the privilege elevation to the echo command in the first case, and the script in the second case.
The echo command is running as root, but it is reporting the current shell user. If you were to modify the code for echo to report its user, it would be root.
I don't know of any elegant method to get the invoker's UID or LOGNAME. I'd probably use a brute-force method of some sort. Hopefully someone else here may know a better trick.

edit: heh, bigearsbilly beat me to it ;-)

Last edited by raconteur; 06-03-2009 at 07:08 PM.
 
Old 06-03-2009, 07:28 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
maybe try the id command?
 
Old 06-03-2009, 10:12 PM   #6
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Original Poster
Rep: Reputation: 70
id does exactly the same thing when called in a script run as sudo
 
Old 06-03-2009, 11:58 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,837

Rep: Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132Reputation: 2132
Code:
 $ man sudo
...
ENVIRONMENT
...
SUDO_USER       Set to the login of the user who invoked sudo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
echo current user with PHP genderbender Programming 1 05-10-2006 06:03 PM
No echo for user id when logging in LORDL Linux - General 1 07-28-2005 12:21 PM
echo user password outspoken Linux - General 4 02-17-2005 05:36 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration