Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to obtain home dir of any user with echo

echo ~puchuu
>> /home/puchuu

But I cant use variable

echo ~$USER
>> ~puchuu
echo `echo ~$USER`
>> ~puchuu
share|improve this question
up vote 28 down vote accepted

You can use eval:

eval echo ~$USER
share|improve this answer
1  
In bash eval isn't needed it with just echo ~$username it's okay, but in sh eval is needed if is a variable – Felipe Alcacibar Nov 25 '15 at 14:23
    
This sometimes gives the wrong value, maybe the home folder of a previous account with the same username? – Andrew MacFie Dec 12 '15 at 18:28
    
@AndrewMacFie: What do you mean by "previous"? – choroba Dec 12 '15 at 18:49
    
@choroba Add a user, delete the user, then add a user with the same username. If the user's home folder is different the second time, this command gives the original home folder rather than the current one. glenn jackman's answer gives the current one. – Andrew MacFie Dec 12 '15 at 18:59

This might work for you:

homedir=$( getent passwd "$USER" | cut -d: -f6 )
share|improve this answer
    
Great; I didn't even know about "getent" – David Tonhofer Feb 2 '14 at 20:35

It seems you are that user -- why not

echo $HOME

?

share|improve this answer
1  
@DavidPostill, I read question AND answers (especially accepted one). Please tell me how you get directory for user other than yourself by running eval echo ~$USER? I am all ears -- last time I checked, $USER evaluates to current user, meaning yourself. – greenoldman Dec 18 '14 at 17:04
    
@Arjan It appears that the question was not so clear ... :/ – DavidPostill Dec 18 '14 at 21:53
    
Yeah, that is what i've looking for – deFreitas Apr 27 '15 at 2:13
1  
This won't work if you are in a sudo'ed environment and did not pass sudo the -H or -i flags - $HOME will still be the previous (sudo'ing) user's home directory. – Asfand Yar Qazi Jun 12 '15 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.