bash - have to determine all users home directories - tilde scripting problem -


assume someuser has home directory /home/someuser

name=someuser

in bash - expression use combining tilde (~) , $name return users home directory?

homedirectory=~someuser echo $homedirectory /home/someuser name=someuser echo ~$name ~someuser 

any suggestions?

safer:

eval homedirectory="$(printf "~%q" "$name")" 

here %q option printf quotes , escapes dangerous characters.

if $name joe, you'd /home/joe. root, might /root. "abc;rm something" you'd "~abc;rm something" instead of having removed.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -