Most shells have a shortcut for the pathname to
your home directory: a tilde (~
), often
called "twiddle" by Unix-heads. You can use ~
in a pathname to the home directory from wherever you are. For example, from any
directory, you can list your home directory or edit your .cshrc file in it by typing:
%ls ~
... %vi ~/.cshrc
If you're
using a very old Bourne shell, one that does not support the tilde convention,
try the $HOME
or $LOGDIR
variables instead.
You could change your current directory to your home directory by typing
cd ~
or cd
$HOME
, but all shells have a shorter shortcut: typing plain
cd
with no argument also takes you
home.
If your shell understands the tilde, it should also have an abbreviation for other users' home directories: a tilde with the username on the end. For example, the home directory for mandi, which might really be /remote/users/m/a/mandi, could be abbreviated ~mandi. On your account, if Mandi told you to copy the file named menu.c from her src directory, you could type:
% cp ~mandi/src/menu.c .
Don't confuse this with filenames like report~. Some programs, like the GNU Emacs (Section
19.4) editor and vi, may create
temporary filenames that end with a ~
(tilde).
Your version of the Bourne shell might also emulate the special "directory" /u — if your system administrator hasn't already set up /u, that is. It's a directory full of symbolic links (Section 10.4) to users' home directories. For instance, /u/jane could be a link to /home/users/jane. Many systems are now using /home for home directories, in favor of the old /usr/users or /u conventions. Darwin uses /Users/username (note the uppercase U!), but the tilde works the same there, too.
If all else fails, here's a trick that's probably too ugly to type a lot, but it's useful in Bourne shell scripts, where you don't want to "hardcode" users' home directory pathnames. This command calls the C shell to put mandi's home directory pathname into $dir:
username=mandi dir=`csh -fc "echo ~$username"`
In fact, using echo (Section 27.5) yourself is a good way to
see how ~
works. Try echo ~
, echo ~/xyz
, echo ~xyz
, and so on. Note that different shells
do different things when ~
user
doesn't match any user: some print an error,
others return the unmatched string.
— JP