In the terminal, one way to reference home is by using ~/
. I don't understand how this works, because the output of file ~/
is:
/home/admin/: directory
and just running ~/
results in:
-bash: /home/admin/: Is a directory
so what's converting ~/
into /home/admin
?
~/
also appears to work in /bin/dash
, so I also know it's not bash specific.
This is tilde expansion.
Tilde expansion is required by POSIX (see that first linked page) and appears in all modern Bourne-style shells. That includes the popular shells
bash
,ksh93
, andzsh
, but also more minimalist shells likemksh
,dash
, andbusybox ash
.In practice, different POSIX-compatible shells sometimes differ in the precise details of tilde expansion, both in the unspecified case that
HOME
would be used but is unset or empty, and to permit~
notation to be used for other purposes than expanding users' home directories. For example, tilde expansion inbash
also provides a shorthand for accessing the values of thePWD
andOLDPWD
variables, with~-
and~+
, respectively.However, in the typical cases, it works about the same across Bourne-style shells. These are typical cases (but note that this way of separating them is not official, it's just my way of presenting the material):
~
or~/
by itself expands to your home directory.~/
followed by more path components expands to a path starting at your home directory.~username
or~username/
by itself expands to the home directory of the user whose username isusername
.~username/
followed by more path components expands to a path starting at the home directory of the user whose username isusername
.~
is 'shorthand' for$HOME
~
is a 'shorthand' way to write$HOME
in other words your home directory. It works in shells (e.g.bash
) and is called 'tilde expansion'.If you add a trailing slash you imply that you are talking about a directory.
Examples: