I want to create a bash script that must be executed with sudo but should take into account the name of the non-sudo user who executed it. So if user bob
runs sudo ./myscript.sh
I would like myscript.sh
to know bob
was the one who executed it.
Let's look inside myscript.sh
:
USER=$(whoami)
# Do something that takes into account the username.
How can I know the name of the user who spawned the process? More specifically, what should I use instead of whoami
to get bob
and not root
?
USER=$(whoami)
. Note that theUSER
already exists as a shell internal variable. Also, if it's a bash script, don't run it usingsh
, which only has a POSIX-compatible subset of features.#!/usr/bin/env bash
on my bash scripts.sh
or something else.