Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have to determine all users home directories - tilde scripting problem

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?

like image 337
iaroot Avatar asked Jan 15 '10 04:01

iaroot


1 Answers

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.

like image 135
Dennis Williamson Avatar answered Oct 16 '22 12:10

Dennis Williamson