Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.10 Variables Help You Find Directories and Files Chapter 14
Moving Around in a Hurry
Next: 14.12 Marking Your Place with a Shell Variable
 

14.11 Finding (Anyone's) Home Directory, Quickly

The C shell, ksh and bash 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

Bourne shell users - try the $HOME or $LOGDIR variables instead.

You could change your current directory to your home directory by typing cd ~ or cd $HOME although 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 /usr3/users/mfg/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 (32.4) editor, create temporary filenames that end with a ~ (tilde).

The Bourne shell doesn't have anything like ~mandi. 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"`

The tilde is a good thing to use in your shell setup files (2.2), too.

- JP


Previous: 14.10 Variables Help You Find Directories and Files UNIX Power ToolsNext: 14.12 Marking Your Place with a Shell Variable
14.10 Variables Help You Find Directories and Files Book Index14.12 Marking Your Place with a Shell Variable

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System