views:

166

answers:

6

Assume someuser has a home directory /home/someuser

NAME=someuser

In bash - what expression to I use combining tilde (~) and $NAME to return the users home directory?

HOMEDIRECTORY=~someuser
echo $HOMEDIRECTORY
/home/someuser
NAME=someuser
echo ~$NAME
~someuser

any suggestions?

A: 

one alternative way

awk -F":" '{print "user: "$1", Home directory is: "$6}' /etc/passwd
ghostdog74
This will fail if e.g. LDAP auth is used.
Ignacio Vazquez-Abrams
A: 

Tilde ( ~ ) it's the same as $HOME so, not all the user will have as root to home the same directory.

But if you insist in using the tilde this do the work:

echo ~/../$NAME

See:

$ pwd
/home/oreyes
$ export NAME=john 
$ export DIRECTORYNAME=~/../$NAME
$ cd $DIRECTORYNAME
$ pwd
/home/john
OscarRyz
This assumes that all users' home directories, including the current one, are siblings. This will fail especially spectacularly if the current user is `root`.
Ignacio Vazquez-Abrams
yeap, that's what I write in the answer...
OscarRyz
No guarantees even when not root. For example, homes on CMU's Andrew are given out following the pattern `/afs/andrew.cmu.edu/usr##/$LOGNAME`, where `##` is a semi-random integer.
ephemient
+2  A: 

Interesting difference between bash and csh, where ~$VARNAME actually does what you'd expect!

This is ugly, but it seems to work in bash:

homedir=`eval "echo ~$USERNAME"`

Now $homedir holds the home directory associated with $USERNAME.

Jim Lewis
That is indeed the way to do it in bash. Tilde expansion happens before parameter expansion, so the shell actually tries to expand ~$USERNAME into the home directory of a user whose name is literally $USERNAME, and not the contents of $USERNAME.
Wilson
Just make sure `$USERNAME` is sanitized first and doesn't contain stuff like `; sudo rm -rf /`, 'kay?
ephemient
@ephemient: Good advice! I'm surprised "little Bobby Tables" hasn't shown up yet!
Jim Lewis
+2  A: 

If you have access to getent:

getent passwd "$NAME" | cut -d: -f 6
Ignacio Vazquez-Abrams
+5  A: 

Safer:

eval HOMEDIRECTORY="$(printf "~%q" "$NAME")"

Here the %q option to printf quotes and escapes dangerous characters.

If $NAME is joe, you'd get something like /home/joe. For root, you might get /root. For "abc;rm something" you'd get "~abc;rm something" instead of having something removed.

Dennis Williamson
+1 for solution. Also, minor typo: should not have trailing grave accent. Should be eval HOMEDIRECTORY="$(printf "~%q" "$NAME")"
Larry K
Thanks. Typo fixed.
Dennis Williamson
A: 

Thank you one and all- the difficulties I FACED were getent isn't available the user accounts are maintained in t third party database, not /etc/passwd

There are user accounts that are derivations of each other (sys and system, etc)

eval works great

iaroot
Welcome to StackOverflow! Please consider accepting one of the answers you found helpful, so that person can have the +15 reputation bonus. (You'll need to do this from the same SO userid you used to post the question.)
Jim Lewis