Click here for important information about

getting username home

Custom / CUSTOM
April 11, 2010 at 03:12:19
Specs: wxp, 2.401 GHz / 2047 MB
As root, I was trying to get the location of the home directory of a given user ... seemed simple, but I can't get it.

I try to put it into a variable, but I keep ending up with "~username" (without the double quotes) inside a variable, and when I then run an mkdir, it doesn't seem to parse the tilde, but it tries to create a directory explicitally like '~username/dubdir` ... and that does not work. I seem to understand that the parsing of tilde+username is done at certain levels only (file and directory manipulation commands), but why doesn't it work when I have a string that is exactly that, but in a variable ?

To summarize, I sort of have this:


And then I try : mkdir $variable1/subir

Second question is: what other way can I do this ?

edit : It's not an WindowsXp question, but *NIX

See More: getting username home

April 11, 2010 at 11:59:47
doesn't $HOME yield the home direcitory? and $USER is the currently logged in name?
i'm not familiar with the tilde format, but i haven't had much exposure to nix.

Report •

April 11, 2010 at 12:31:47
The HOME variable is only set within login of a given user (= each user can query his own home directory), but here I am another user (root) and I simply want to get home-directory of given user. There's other ways, but I want to know why exactly the above problem occurs.

For example, if you are root, you can do this:

cd ~xyz

... which takes you to the home of user xyz (assuming this user exists of course).

Report •

April 11, 2010 at 14:06:59
what about using cd, then pwd?
cd ~xyz
for xx in `pwd` ; do echo $xx ; done
(i know there's easier way to capture output from command, i just can't think of it, haven't messed much in nix lately)

Report •

Related Solutions

April 11, 2010 at 14:17:48
That only works if you are logged in as that user, which I am not

Report •

April 12, 2010 at 07:14:03
The only way I see to get a given user's home directory is to look at the 6th field of the password file:



while IFS=":" read myuname f2 f3 f4 f4 f6 f7
   if [[ "$myuname" = "$unamearg" ]]
      echo "$f6"
done < /etc/passwd

Report •

April 12, 2010 at 08:27:31
That would be the best alternative indeed ... but still, if - as root - you perform "cd ~username", you do end up in the home of that user. Actually, the syntax may have been designed just as an easy way to get to that dir.

I'll have some more checks. If you for example do this : "echo ~username", he does not parse the value. Logically, since the ampersand does not suddenly replace the dollar sign.

Report •

April 12, 2010 at 14:07:25
@tvc, I don't have access to a *nix machine right now, but from
what you say: "if you perform cd ~username you do end up in
the home of that user", what happens if you then type pwd?
Does it give you that user's home directory?

Report •

April 12, 2010 at 14:39:48
Of course, that would work, but I don't like that idea, I would be forced to navigate.

Report •

April 12, 2010 at 14:55:59
A bit late, but this is more in detail on the code I have issue with:

[root@srv001 tmp]# cat ttt


useradd $p_user

echo ~${p_user}
echo '~${p_user}'
echo "~${p_user}"
echo ~newuser

[root@srv001 tmp]# ./ttt
useradd: user newuser exists
[root@srv001 tmp]#

Report •

April 12, 2010 at 17:11:03
I see what you mean now. Up to now I and others have been
barking up the wrong tree.

Try this untested attempt:

sh -c "echo ~${p_user}"

I think this will call sh with the argument "echo ~newuser"
which is exactly what you want it to execute. You can then
capture the output into a new variable.

for xx in `sh -c "echo ~${p_user}"` ; do userhome=$xx ; done

Report •

April 12, 2010 at 23:20:53
i guess i'm still missing the issue by miles, or else ubuntu is different linux (understood Tvc, you're overseas.)
not logged in as root, just me, i put:
(where "somebody" is just a name i got from the primary
servers main users subdirectorys).
echo $xx
and i get (f/e, names changed)
cd $xx
i think it's a version difference amongst the nix

Report •

April 13, 2010 at 01:04:34
I'll test solution proposed in R11

R12 .. that is sort of how I got to raise this thread, only I had just the username in the variable, not the tilde (like echo ~$xx where xx=somebody)

It may be down to OS differences, it seems like I need to explictially put the name of the user, and not allowing any variable to be used at all. He seems to literally want to create ~username (if used with MKDIR for example), and he "forgets" to parse the tilde (when initially supplied with variables).

The other simple solution, is to echo to a temp file, then run the tempfile. I use this solution a lot, when the problems starts to bug me (get it, bug me ... ).

Report •

April 13, 2010 at 01:39:23
ah, the lightbulb lighteth... Lol!
VARIABLE after the tilde - not explicit. DUH to me!
work-arounds are just that: let you get on with it...

Report •

April 13, 2010 at 02:03:52
So in that case, can't you do this:

echo $p_user (expect to see: newuser)
echo ~${p_user} (expect to see: ~newuser)
echo ${p_user} (expect to see /home/newuser)

Report •

April 13, 2010 at 17:54:48
re: #14: i tested that on linux and it wouldn't fly.
i figure there's some obscure command that will handle this situation, (or build-it-yourself) but no clue. Obviously, something builtin to the shell does it, but just not using var.expansion

Report •

April 14, 2010 at 06:16:49
I've just done some testing on Linux and the following is the most succinct I could manage:

userhome=`sh -c "echo ~$p_user"`
echo $userhome

Report •

May 1, 2010 at 15:01:50
Response 3 works, but not ideal
Response 10 and 16 work perfectly !

R14 indeed does not work

Report •

Ask Question