Get home directory by username

86

15

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

puchu

Posted 2012-10-06T15:17:31.963

Reputation: 1 202

Answers

90

You can use eval:

eval echo ~$USER

But see Andrew's comment and glenn's reply below.

choroba

Posted 2012-10-06T15:17:31.963

Reputation: 14 741

8In 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 – 2015-11-25T14:23:08.433

This sometimes gives the wrong value, maybe the home folder of a previous account with the same username? – Andrew – 2015-12-12T18:28:58.450

@AndrewMacFie: What do you mean by "previous"? – choroba – 2015-12-12T18:49:39.690

1@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 – 2015-12-12T18:59:16.477

Note also that if you eval echo "~$USER" you are making the assumption that $USER does not contain special characters that the shell may interpret. For example, if USER="foo\$bar", then when we eval the shell will substitute $bar into your output which is not what you want. Basically, if you take this route, you need to make sure that $USER is sane input. Most of the time it probably will be, but you should bear this in mind. – Zorawar – 2017-03-30T01:51:25.093

if you want to assign it to a variable, use this: userdir=$(eval echo ~$SOMEUSER) – Michael Potter – 2017-12-07T03:00:22.713

Can confirm that this won't work at all if you have anything other than letters and numbers in a username. The method provided by Evan Carroll & Glen Jackmans answer below appears to work at least on Ubuntu 18. E.g:

$( getent passwd "john-smith" | cut -d: -f6 ) – Sk446 – 2019-11-25T11:33:53.023

68

This might work for you:

homedir=$( getent passwd "$USER" | cut -d: -f6 )

This will also work on users that are not you. For instance,

homedir=$( getent passwd "someotheruser" | cut -d: -f6 )

glenn jackman

Posted 2012-10-06T15:17:31.963

Reputation: 18 546

This is legit, using getenv rather than assuming the location of passwd is even a step further than assuming the location of home is /home/ – Evan Carroll – 2016-12-01T22:19:50.047

14

It seems you are that user -- why not

echo $HOME

?

greenoldman

Posted 2012-10-06T15:17:31.963

Reputation: 518

3This 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 Qazi – 2015-06-12T11:32:37.763

1

I don't know if it helps, but placing the tilde outside the expression works on ZSH but not on Bash:

echo ~`echo $USER`

Pedro Penna

Posted 2012-10-06T15:17:31.963

Reputation: 111

0

Once you login, run cd to go to your home directory, then run pwd to print the working directory.

Vinoth Hari

Posted 2012-10-06T15:17:31.963

Reputation: 21

That's not going to work; the idea here is to do this in shell script, and presumably without requiring the credentials of the user in question... – SamB – 2016-08-27T04:57:39.090

-1 for the same reason SamB mentioned. I edited your answer because I want you to see how to use Markdown formatting, and to be more concise. You also missed the much easier method, which is just echo $HOME. – wjandrea – 2016-09-04T22:35:09.027