0

According to this post, I run this from the command line:

USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)

and get the following output:

/root
/usr/sbin
/bin
/dev
/bin
/usr/games
/var/cache/man
/var/spool/lpd
/var/mail
/var/spool/news
/var/spool/uucp
/bin
/var/www
/var/backups
/var/list
/var/run/ircd
/var/lib/gnats
/nonexistent
/var/lib/libuuid
/home/user
/var/run/vboxadd
/var/lib/puppet
/var/run/sshd

When I run this in a script (as sudo, which is the point of the whole thing --- as sudo, ~ expands to /root):

USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
echo $USER_HOME

I get my correct path /home/user.

Why can I not invoke my function manually to get the same output?

3
  • 2
    This is probably better asked over at unix.stackexchange.com Commented Jan 11, 2014 at 20:58
  • This command is designed to run by sudo. How are you running it from the command line? Commented Jan 11, 2014 at 21:01
  • I don't understand. When I run getent passwd user | cut -d: -f6 as an ordinary for any value of user, I just get the user's home directory, not all of the stuff you cited. What is the value of $SUDO_USER? Commented Jan 11, 2014 at 21:08

1 Answer 1

5

Because when not using sudo, $SUDO_USER is not set and you get the output of getent passwd without further argument, which lists all users. The cut then extracts the home directory part.

Replace $SUDO_USER with $USER when not running with sudo.

Note that using getent passwd ${SUDO_USER:-$USER} should work in both cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.