4
I can't figure out how to prefix a string (e.g. "user1") with a tilde (~) to produce ~user1
and have that expand to user1's home directory.
Here is what I have now:
USER="user1"
ls ~${USER}
ls: cannot access ~user1: No such file or directory
~user1
is not being expanded. How can I achieve it?
edit
This does not work:
$ cat test.sh
USER="user1"
ls $(~$USER)
$ sh -x test.sh
+ USER=user1
+ ~user1
test.sh: 1: ~user1: not found
+ ls
test.sh
Using
/home/user1
is not an option? – terdon – 2012-09-14T15:34:46.560Don't use USER it is your name. Instead use lower case variables. – ctrl-alt-delor – 2012-09-14T15:43:21.703
why should lower case be preferred? – bernie – 2012-09-14T15:47:09.407
3so you don't overwrite the built in variables such as USER. USER holds the name of the current user that is executing the command. – ctrl-alt-delor – 2012-09-14T15:52:43.223
ok so USER in particular is problematic, not all uppercase var names are. Thanks – bernie – 2012-09-14T15:53:35.627
1yes but do you want to learn them all, and then keep up to date with all new ones. For this reason it is customary to have all local variables in shell scripts as lower-case. – ctrl-alt-delor – 2012-09-14T16:03:01.173