How to get a user's home directory from the username in a variables in Bash?

In Bash, ~username gives the username user's home. However, this does not work for situations where username is in a variable such as


How to get the home directory from the user name in a variable in Bash?

asked Feb 10, 2016 by anonymous

1 Answer

You can use this Bash script snippet:

userhome=$(eval echo ~$user)

Here, $user get evaluated to its value (say VALUE) and then ~VALUE will be evaluated again by eval which gives the same effect as ~username.

answered Feb 13, 2016 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost