3

Say I have the following bash script:

#!/bin/bash

echo $( whoami )

This is what it outputs with and without sudo:

alix@netbook:~$ ./test.sh 
alix
alix@netbook:~$ sudo ./test.sh 
root

The script executes several commands that require superuser priviledges, but I also want to add the original executing user to a group. Is there any command that returns alix if I run sudo ./test.sh?

1
  • Just noticed that this would better fit Unix.SE, feel free to migrate if needed.
    – Alix Axel
    Jan 27, 2013 at 11:56

1 Answer 1

5

Found the solution in a related answer:

#!/bin/bash

echo $( whoami )
echo $SUDO_USER

And the output:

alix@netbook:~$ ./test.sh 
alix

alix@netbook:~$ sudo ./test.sh 
root
alix

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .