How is it that I've been writing shell scripts for over 15 years now to automate processes, and I've only just discovered $SUDO_USER today? I feel like that's a MASSIVE oversight.

@twann @mike can either of you provide a link, or personal clarification what that is and how to use it?


@RyuKurisu Sure. Here's a script:


echo This is a script.
echo $SUDO_USER ran it.

If you run this as yourself, the output is:

This is a script
ran it.

If you run it with sudo, the output is:

This is a script
<username> ran it.

Where <username> is the account name you ran the script from. So, if my username is mike, it would say "mike ran it".

This really helps out with things like logging.


Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.