the life of a sysadmin.
Carousel is a lie!

Linky:
[FSF Associate Member] LOPSA

Email: aardvark at saintaardvarkthecarpeted dot com

Bash: How to get the home directory of a user in a variable

Sat Jul 28 17:30:50 EDT 2007

Not the clearest title…what I mean is, I was writing a Bash script like this:

SOMEONE=foo
...
mv $SOMETHING ~$SOMEONE

only it kept failing with "~foo: No such file or directory". I had a look at the manual, but it wasn't terribly clear. In a nutshell, though, Bash wasn't doing the tilde expansion no matter what combination of braces and quotes tried.

Eventually, I came across a pirated copy of the ever-excellent Unix Power Tools, 3rd Ed. (mine's at work, or I'd've checked it a lot sooner), and it had a solution:

FINAL_RESTING_PLACE=$(/bin/bash -c "echo ~${SOMEONE}")

I'm sure there must be a better way of doing this, though…Woot, any suggestions?

(permalink) (comments)


Comment from Chris Kilgour
Date: Sat, 28 Jul 2007 18:19:33 -0700

try back-ticking:

SOMEONE=foo
ls `~$SOMEONE`

Comment from Chris Kilgour
Date: Sat, 28 Jul 2007 18:22:00 -0700

Bzzzt. I take that back (hangs head in shame).


Comment from tobutaz
Date: Sun, 29 Jul 2007 12:36:13 +0200

Try this:

eval "SOMEWHERE=~$SOMEONE"

Comment from Saint Aardvark
Date: Sun Jul 29 22:14:29 EDT 2007

Tobutaz, you've got it. I should've thought about eval.

Many thanks!