SOMEONE=foo ... mv $SOMETHING ~$SOMEONE
Linky:
Email: aardvark at saintaardvarkthecarpeted dot com |
Bash: How to get the home directory of a user in a variableSat 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? Comment from Chris Kilgour try back-ticking: SOMEONE=foo ls `~$SOMEONE` Comment from Chris Kilgour Bzzzt. I take that back (hangs head in shame). Comment from tobutaz Try this: eval "SOMEWHERE=~$SOMEONE" Comment from Saint Aardvark Tobutaz, you've got it. I should've thought about eval. Many thanks! |