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?
Interesting difference between bash and csh, where ~$VARNAME actually does what you'd expect!
This is ugly, but it seems to work in bash:
Now $homedir holds the home directory associated with $USERNAME.
If you have access to
getent
:BEST METHOD
Required: nothing (n.b., this is the same technique as getent without requiring getent)
NICE METHOD FOR ROOT LINUX
Required: Linux, root (or sudo)
SOLUTION FOR COMPLETE EXPANSION
Example usage:
METHOD FOR HARNESSING CSH
This is a BASH script, it just calls csh.
Required: csh
METHOD OF DESPERATION
Required: finger (deprecated)
You can combined the grep operation into sed, but since this method is sucky, I wouldn't bother.
one alternative way
Safer:
Here the
%q
option toprintf
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.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:
See: