OpenSuse V13.2-64, KDE
Where is ~/ ?
Is it the same as $HOME ?
OpenSuse V13.2-64, KDE
Where is ~/ ?
Is it the same as $HOME ?
Yes.
~ is a “shortcut” that expands to $HOME.
And ~user expands to user’s $HOME.
I.e. ~test would be the same as /home/test/.
Actually, I think that last example isn’t accurate…
“~something” points to the home directory of User named “something” so you need to place a forward slash between the tilde and the name to point to a subdirectory of the User Home directory.
Unless I’m missing something (And I did some quick testing before posting)…
TSU
You are wrong.
~/username would point to /home/myself/username, not /home/username.
~username does point to /home/username (or whatever the home directory of the user “username” would be)
See the difference?
Also have a look at: Tilde - Wikipedia
(And I did some quick testing before posting)…
And what exactly did you test?
[Edit]On reading your statement a third time, you seem to mean that if you want to specify the folder xxx in your home directory, you have to use ~/xxx.
That is true of course, but I haven’t mentioned anything else.
What I wrote was the ~xyz would point to the home directory of user “xyz”.
I don’t know what is supposed to work with your example as follows
I.e. ~test would be the same as /home/test/.
If you open a console and run the following, I don’t think that you’ll be able to get it to return anything successfully unless you have a User called “test” and even then you won’t get the result you suggested. Maybe I should be testing your syntax using something else?
ls ~test
TSU
henk@boven:~> ls -l ~marian
totaal 40
drwxr-xr-x 2 marian wij 4096 1 okt 2007 bin
drwxr-xr-x 2 marian wij 4096 8 jan 2015 Desktop
drwx------ 2 marian wij 4096 1 mei 2012 Documents
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Downloads
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Music
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Pictures
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Public
drwxr-xr-x 2 marian wij 4096 1 okt 2007 public_html
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Templates
drwxr-xr-x 2 marian wij 4096 20 feb 2012 Videos
henk@boven:~>
As you can see the above is done by user henk and it shows the contents of the home directory of user marian (which is /home/marian btw, as configured in /etc/passwd). Exactly as Wolfi323 explains. And exactly as it is explained in the bash man page under ~ expansion:
Tilde Expansion
If a word begins with an unquoted tilde character (`~'), all of the characters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the shell parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.
If the tilde-prefix is a
~+', the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is a
~-‘, the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a+' or a
-’, the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the tilde-prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading+' or
-‘, `+’ is assumed.
If the login name is invalid, or the tilde expansion fails, the word is unchanged.
Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is also performed. Consequently, one may use file names with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value.
And this is nothing new. It was already implemented in the original sh shell on Unix ages ago.
And ~/ is expanded as the home directory of the user typing it (or more precise: the user that owns the bash process that executes this command), and thus in my case is the same as ~henk/, both expand as /home/henk/ (because that is defined as the home directory of henk in /etc/passwd).
..... explained in the bash man page under ~ expansion:
Nice, thank you.
Hi,
So just to make it clear that
~
and
$HOME
are equal, the only difference is that the ~ does not expand inside quotes, it does not matter if it is double or single quotes. so
echo "~"
does not expand while
echo "$HOME"
does expand properly and that is a common mistake when someone tries to put it in a variable
var="~/some/directory"
does not do what is expected.
While I understand what you try to explain (in fact the very first ± 10 words of my man page quote above), not all above is precise
~
is a replacement for $HOME only when it is the first character of a word and a / follows it immediatetly:
~/
And then only when HOME is set (which btw will be the case more often then not). Else it will be replaced by the information found in /etc/passwd.
I am here only repeating what I quoted above from the man page in a bit different wording.
OK,
I see what’s happening…
Perhaps, it’s just me but I do my testing using a collection of descriptive namespaces…
Whenever I use “test” it’s for something generic without any special characteristics.
If I use a namespace that has some significant characteristic, I always use a namespace that hints at that special characteristic.
That’s why you’ll see in all my posts namespaces like packagename, username, user etc. and typically italicized. You’ll note that in each of these namespace examples, some generic name <could> have been used but then the reader could easily use some other name and fail… for instance, for all those zypper commands if I used something generic the reader might either not substitute the name or type in an application name, both which would almost certainly fail.
In this case where wolfi used the namespace “test” it can <only> point to a text string associated with an active and working User account. If you simply create an ordinary folder using that name, the command fails. If it points to <any> other than a valid User’s home folder, the command will fail. And, this is why I said that unless you create a User called “test” the command won’t work literally (without substituting the “test” name).
TSU
Actually ~ alone without a following / works as well:
test@amiga:~/Downloads/home:wolfi323:branches:KDE:Frameworks5/kcron5> ls ~
Desktop Downloads GNUstep kdenlive Mail rpmbuild tmp
test@amiga:~/Downloads/home:wolfi323:branches:KDE:Frameworks5/kcron5>
Of course, because ~*username *points to the home directory of user username, as explained.
It should be clear that this can only work if *username *is an actual user on the system.
And, this is why I said that unless you create a User called “test” the command won’t work literally (without substituting the “test” name).
That’s why I wrote “user” in italics:
IMHO it should be obvious that user is to be substituted with an actual username here…
Correcct, because
(or all characters, if there is no unquoted slash)
It illustrates once more my message: Read the man page and don’t rely on all sorts of (mis)interpretations people try to tell you.