UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.12 Marking Your Place with a Shell Variable Chapter 14
Moving Around in a Hurry
Next: 14.14 Automatic Setup When You Enter/Exit a Directory
 

14.13 Which Directory Am I in, Really?

The C shell, and some other shells too, keep their own idea of what your current directory is. The csh will give you the current directory's absolute pathname in $cwd; bash uses $PWD. But sometimes this can give you the wrong pathname.

Why? Because the cwd variable was added before many versions of UNIX had symlinks (18.4) (symbolic links). As article 18.7 explains, symlinks can point to directories any place else on the filesystem or even (for some UNIXes) directories on another computer. Poor cwd couldn't cope: it assumed that the current directory was the name of the symlink itself (instead of the directory that the link points to). That led to problems like the one below: cding to a "directory" named wpa that's actually a symlink to /work/pwrtools/articles. The value of $cwd, shown in the prompt, is wrong. The /bin/pwd command shows the real current directory (14.4) (you should type all of /bin/pwd because some shells and users have plain pwd aliased to do echo $cwd):

/home/jerry% pwd
/home/jerry% ls -l wpa
lrwxrwxrwx  1 jerry  23 Sep 8 13:55 wpa -> /work/pwrtools/articles
/home/jerry% cd wpa
/home/jerry/wpa% /bin/pwd
/work/pwrtools/articles
/home/jerry/wpa%

By now, a lot of C shells have a variable named hardpaths; the bash variable is nolinks. If you set the shell variable (usually in your shell setup file (2.2)), the shell won't be fooled by symlinks. Watch:

/home/jerry/wpa% cd
/home/jerry% set hardpaths      (on bash, nolinks=1)

/home/jerry% cd wpa
/work/pwrtools/articles%

Setting hardpaths or nolinks makes the shell do extra work, so don't bother with it unless you use $cwd.

The dirs (14.6) command has the same problem. Setting hardpaths or nolinks helps there, too.

If your system has symlinks but your shell doesn't recognize a variable like hardpaths, here are workarounds for the .cshrc file:

alias setprompt 'set prompt="${cwd}% "'
alias cd        'chdir \!* && set cwd=`/bin/pwd` && setprompt'
alias pushd     'pushd \!* && cd .'
alias popd      'popd \!* && cd .'

When you cd, that alias resets the cwd variable to the output of /bin/pwd, then resets the prompt to the new cwd. Using pushd or popd (14.6) runs the cd alias, too - this changes to the current directory (.), which fixes cwd (as well as the dirs command) and resets the prompt.

Whew. Are symlinks worth the work? (I think they are.)

- JP


Previous: 14.12 Marking Your Place with a Shell Variable UNIX Power ToolsNext: 14.14 Automatic Setup When You Enter/Exit a Directory
14.12 Marking Your Place with a Shell Variable Book Index14.14 Automatic Setup When You Enter/Exit a Directory

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


Banner.Novgorod.Ru