In a small bash script I'm running I am attempting to chown a new directory that is created. I've added:

sudo chown $USER:$USER /var/www/$sitename
sudo chmod 775 /var/www/$sitename

after the line where I mkdir (sudo mkdir /var/www/$sitename).

For some reason the chown is not executing. I can execute it manually but when written in the file it doesn't work. I have noticed that "chown" is not highlighted in the same color as "mkdir" and "chmod" but I can't figure out my problem.

Why doesn't chown work here?

Is it an issue with $USER:$USER?

EDIT Here is the full script. How would I chown the file to whichever non root user executed the script?

# New Site

cd /etc/apache2/sites-available/
echo "New site name (test.my):"
read sitename
echo "<VirtualHost *:80>

        ServerAdmin admin@$sitename

    ServerName $sitename

        ServerAlias $sitename

    DocumentRoot /var/www/$sitename

        <Directory />
                Options FollowSymLinks
                AllowOverride All
        <Directory /var/www/$sitename>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>" > $sitename.conf
sudo mkdir /var/www/$sitename
sudo chown $USER:$USER /var/www/$sitename
echo USER is $USER
sudo chmod 775 /var/www/$sitename
sudo a2ensite $sitename.conf
sudo apachectl restart
echo "New site created"
  • 1
    Is there a group named $USER? getent group $USER – Cyrus Feb 5 '15 at 20:20
  • 1
    $USER variable is set during interactive login. How do you run your script - from login session or using cron or from daemon? – myaut Feb 5 '15 at 20:31
  • 1
    Check if the USER variable is even seen by the script. If you add a line to your script that says echo USER is $USER, what does it print out? – Mark Plotnick Feb 5 '15 at 22:12
  • @MarkPlotnick Evidently, USER is root. With the edit I've made do you think you can explain how to chown the file to whichever non-root user executes the script? – BrassApparatus Feb 7 '15 at 9:41
  • @myaut I run it manually from the terminal. – BrassApparatus Feb 7 '15 at 9:44

If, for some reason, $USER is not set, you can use the id command to obtain the identity of the real user. So the first time you use the $USER variable, you can use the shell expansion to supply a default value. Change the chown line in your script to:

sudo chown ${USER:=$(/usr/bin/id -run)}:$USER /var/www/$sitename

If USER is empty or unset when this is run, bash will set the USER variable to the output of /usr/bin/id -run


When I calling my script with sudo it would set $USER to root.

$ sudo ./myscript.sh

I tried the chown ${USER:=$(/usr/bin/id -run)}:$USER /var/www/$sitename but it would still return root.

I found if I used who with awk I was able to get the current user that called the script with sudo.

currentuser=$(who | awk '{print $1}')}
chown -R $currentuser:$currentuser /var/www/$sitename`
  • 1
    To prevent setting currentuser (sometime) with multiline users, use $(who | awk 'NR==1{print $1}') instead. – Illuminator Feb 12 '18 at 12:31
  • I am trying to change /usr to my user: chown -R $currentuser:$currentuser /usr - does not work - still shows root:root – Sam-T Dec 23 '19 at 3:34

In order to simplify the problem and since your are getting the variable sitename, why don't you read a username variable?

With that you'd make sure that the script execution is not dependent on the environmental variables made available the way the script is executed.


There is only a tiny fault I think. sudo opens a new shell for executing the command and after sudo the user is root. So maybe you should use something like this:


sudo chown $MYUSER:$MYUSER

as i think MYUSER is not systemspezific overwritten and shall work.

  • 1
    The variable is evaluated before the command is executed, so your suggested alternative would make no difference. (Try it with echo chown... and see.) – roaima Jul 9 '16 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.