ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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:
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.