SUMMARY: pwd vs. $cwd

From: Jack Reiner (jjr2@edi-nola.com)
Date: Fri Feb 09 1996 - 11:37:48 CST


Original question:

>Hi All.
>
>Here is a squirrelly one that took me by surprise. Apparently, there is a
>subtle difference between pwd and $cwd in the csh.
>
>Most of the time, typing in pwd and echo $cwd results in identical responses.
>However, on certain mounted directories, pwd seems to give the full pathname
>and echo $cwd only gives the mounted pathname.
>
>Example:
>
>host1:/home/sarah % df -k
>Filesystem kbytes used avail capacity Mounted on
>/dev/dsk/c0t1d0s6 901526 802088 9288 99% /export/home
>/export/home/sarah 901526 802088 9288 99% /home/sarah
>
>host1:/home/sarah # mount -F nfs host2:/data /home/sarah/jane/a
>
>host1:/home/sarah % pwd
>/home/sarah <----------\
> \ same, as expected
>host1:/home/sarah % echo $cwd /
>/home/sarah <----------/
>
>host1:/home/sarah % cd jane/a/b
>
>host1:/home/sarah/jane/a/b % pwd
>/home/sarah/jane/a/b <--------------\
> \ still the same :-)
>host1:/home/sarah/jane/a/b % echo $cwd /
>/home/sarah/jane/a/b <--------------/
>
>
>
>host1:/home/sarah # umount /home/sarah/jane/a
>
>host1:/home/sarah # mount -F nfs host2:/data /export/home/sarah/jane/a
>
>
>
>host1:/home/sarah % pwd
>/home/sarah <----------\
> \ same, as expected
>host1:/home/sarah % echo $cwd /
>/home/sarah <----------/
>
>host1:/home/sarah % cd jane/a/b
>
>host1:/home/sarah/jane/a/b % pwd
>/export/home/sarah/jane/a/b <--------------\
> \ different!
>host1:/home/sarah/jane/a/b % echo $cwd /
>/home/sarah/jane/a/b <--------------/
>
>
>
>What gives? I have not been able to find any documentation on this in man page,
>Answerbook, Nemeth's UNIX Sys Admin Handbook, or Frisch's Essential Sys Admin.
>
>This is on Solaris 2.3, kernal patch 101318-70, SPARCserver 1000.
>
>Is this due to a difference between pwd and $cwd? Is there perhaps a subtlety
>about mounted disks of which I am not aware? (entirely possible :-))
>
>Although this is not a crash-and-burn situation, it is giving one of our
>programmers fits.
>
>Thank you.
>
>Regards,
>Jack Reiner Structural Analysis Computer System
>(504) 443-5481 Engineering Dynamics, Inc.
>jjr@edi-nola.com 2113 38th Street
>http://www.sacs.co.uk Kenner, LA 70065 USA
>

-----------------------------------------------------------------

From: Judy Z. Huang <zh@cc.bellcore.com>

If you use /bin/ksh, then $PWD always contain the 'pwd` result.

-----------------------------------------------------------------

From: bismark@alta.jpl.nasa.gov (Bismark Espinoza)

PWD goes back to the top of the tree.
CWD just remembers the 'cd' commands.

-----------------------------------------------------------------
From: kwthomas@wizard.nssl.uoknor.edu (Kevin W. Thomas)

"pwd" is /bin/pwd. This program tells you are, and doesn't care where you've
been.

"$cwd" is built in to "csh", and tells where it thinks you are. "Csh" has no
way of knowing that the mount point changed. In fact, you should be happy
that it would work. I would think you would get "stale file handle" errors.

It is possible to get different answers from "pwd" and "$cwd" for identical
directories when the automounter is used (at least in Solaris 1.x) on a
client workstation.

I have put the line "alias pwd echo \$cwd" in my .cshrc.

-----------------------------------------------------------------
From: Jay Lessert <jayl@lattice.com>

Nothing new here, your programmers must not be old Unix hands...

$cwd is your shell remembering how you got there.

pwd tells you where you really are. It traverses the entire structure
up to root, then tells you how it got there.

It is instructive to cd into a symlink, then do a $cwd and pwd. Which
one correctly predicts what a "cd .." will do?

-----------------------------------------------------------------
From: adam@ltx.com (Adam Nevins)

Hi,

I don't think what you are seeing is a bug.

There is a command called /bin/pwd. This command figures out what the
current directory is by looking up the name of the current directory ( . ),
the parent directory ( .. ), the parent's parent ( ../..) etc. It will
always show all of the mount points.

ksh and csh keep track of the current directory as you move around. This is
the value you get when you do 'echo $pwd' in csh, 'echo $PWD' in ksh, or
'pwd' in ksh. Although csh also has a built-in called 'pwd' it appears that
it really calls /bin/pwd.

With so many ways to get into some directories, it's hard to figure out where
you are in a consistent way. I was talking about this just today with
some programmers here. They are having a problem when using nfs mounted
directories which live on machines which have more than one hostname. It
gets real confusing real fast.

I hope I've helped to clear some things up for you, although I doubt I have
told you anything truly useful. I was thinking about posting an inquiry
about this same issue. e.g. Is there an accepted method for figuring out
the REAL directory you're in? I'd appreciate it if you would let me know
if you can an authoritative answer about this.

-----------------------------------------------------------------
From: baldwinj@mailbox.ne.tpd.eds.com (John Baldwin)

Here is some info that might help answer your question, taken from Unix Power Tools...
How does Unix find your current directory?

A command like pwd inherits the current directory of the process that started it (usually a shell). It could be started from anywhere. How does pwd find out where it is in the filesystem?

Ex. /usr/joe

The current directory doesn't contain its own name, so that doesn't help pwd. But it has an entry named . (dot), which gives the inode number of the directory.
The current directory has i-number 234, Next pwd ask Unix to open the parent directory file, the directory one level up, through the relative pathname (..). It's looking for the name that goes with i-number 234, Aha: the current directory is named joe. So the end of the pathname must be joe.

Next step pwd looks at the . entry in the directory one level up to get its i-number:14. Like always the name of the one-level up directory is in its parent(..,i-number 12). To get its name, pwd opens the directory two levels up and looks for i-number 14: usr. Now pwd has the pathname usr/joe.

Same steps: look in the parent, i-number 12 . What's its name? Hmmm. The i-number of its parent 12 is the same as its own--and ther's only one directory on the filesystem like this: the root directory. So, pwd adds a slash to the start of the pathname and it's done: /usr/joe

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 full pathname in $cwd. But sometimes it can give you the wrong pathname.

Why? Because the cwd variable was added before many versions of UNIX had symbolics links. Poor cwd couldn't cope: it assumed that the current directory was the name of the symbolic link itself(instead of the directory that the link points to)..To prevent the shell from being fooled by symbolic links set the variable hardpaths....

Check man csh for additional info....

-----------------------------------------------------------------
From: Scott Williamson <scott@dreghorn.demon.co.uk>

Yes, the difference is that $cwd will give you the path that the shell
took to get to that directory because it doesn't know any better. pwd
will give the real physical directory because it starts at the current
directory and works back up the hierarchy.

So symbolic links and mounting or re-mounting directories will confuse
$cwd.

-----------------------------------------------------------------
From: Bill Reed <reedwv@stp.xfi.bp.com>

The var $cwd is set by the shell and does is it not simply remember the
pathname from the last cd command?
Whereas pwd is an external program "which searches back up the
directory tree to report
the true pathname"
-----------------------------------------------------------------
Thanks to all who replied.



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:53 CDT