Discussion:
bash scripts: using ~<username>
(too old to reply)
Angel Tsankov
2006-09-17 20:28:17 UTC
Permalink
When typed at the bash prompt, ~<username> is expanded to the
home directory of the specified user. Whay cannot I use this
construct in scripts? Or, if I can, then how?
Dances With Crows
2006-09-17 20:43:38 UTC
Permalink
["Followup-To:" header set to comp.os.linux.misc.]
When typed at the bash prompt, ~<username> is expanded to the home
directory of the specified user. [Why can'] I use this construct in
scripts? Or, if I can, then how?
Works for me:

clairissa:~$ cat temp.sh
#!/bin/bash
cd ~guest
pwd
clairissa:~$ ./temp.sh
/home/guest

...maybe you'd better post an example of how it doesn't work for you,
with the exact command that's getting executed. Remember, when
debugging shell scripts, "set -x" can give you a whole lot of
information.
--
"Dreams? Best leave dreams to those that can afford them."
--Aunt Cordelia, _Wizard and Glass_, Stephen King
There is no Darkness in Eternity/But only Light too dim for us to see
Michal Nazarewicz
2006-09-17 21:13:46 UTC
Permalink
When typed at the bash prompt, ~<username> is expanded to the home
directory of the specified user. Whay cannot I use this construct in
scripts? Or, if I can, then how?
Dunno what you ask really, but maybe that's what you want:

#v+
FOO_HOME="$(echo ~foo)"
#v-

or maybe even:

#v+
homedir () { eval echo "~$1"; }
FOO_HOME="$(homedir foo)"
#v-

In general, as was noted already, you can use things like:

#v+
cd ~foo
mv file ~bar/file
# ... and so on
#v-
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Chris F.A. Johnson
2006-09-17 21:34:43 UTC
Permalink
Post by Angel Tsankov
When typed at the bash prompt, ~<username> is expanded to the
home directory of the specified user. Whay cannot I use this
construct in scripts? Or, if I can, then how?
dir=~user

Not:

dir="~user"
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Whoever
2006-09-18 02:16:26 UTC
Permalink
When typed at the bash prompt, ~<username> is expanded to the home directory
of the specified user. Whay cannot I use this construct in scripts? Or, if I
can, then how?
Why not use $HOME instead?
Chris F.A. Johnson
2006-09-18 02:32:31 UTC
Permalink
Post by Whoever
When typed at the bash prompt, ~<username> is expanded to the home directory
of the specified user. Whay cannot I use this construct in scripts? Or, if I
can, then how?
Why not use $HOME instead?
Because that's not the same. $HOME is one's own home directory;
~user is user's home directory.
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Bill Marcum
2006-09-18 02:37:58 UTC
Permalink
["Followup-To:" header set to comp.os.linux.misc.]
On Sun, 17 Sep 2006 19:16:26 -0700, Whoever
Post by Whoever
When typed at the bash prompt, ~<username> is expanded to the home directory
of the specified user. Whay cannot I use this construct in scripts? Or, if I
can, then how?
Why not use $HOME instead?
$HOME is your own home directory. If you want to refer to another
user's home directory, you use ~username.
--
Have a taco.
-- P.S. Beagle
Loading...