Subject: Re: bug#34447: `pwd` doesn't show real working directory if directory is renamed by another session

From: Bob Proulx <[email-masked]>
Date: Fri, 22 Feb 2019 16:23:33 -0700

tag 34447 + notabug
close 34447
thanks

Hello Chris,

Chris Wright wrote:
> I found that if a session's working directory is renamed or moved,
> `pwd` doesn't show the real working directory.

Thank you for your bug report.  However I think the shell's built-in
pwd is being confused with the external pwd command.  The shell
internal command has the behavior your describe, intentionally.  The
external one in GNU Coreutils does not.

> ~/test $ pwd
> /Users/<user>/test

The above is using the internal shell builtin.

  $ type pwd
  pwd is a shell builtin

  $ type -a pwd
  pwd is a shell builtin
  pwd is /bin/pwd

The bash shell built-in has this to say about the internal pwd.

  $ help pwd
  pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L        print the value of $PWD if it names the current working
                directory
      -P        print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.

Therefore by default the shell's buitin pwd simply prints out the PWD
environment variable, which has not changed.  This is to preserve the
"logical" (not physical) directory tree based upon how the process got
there, intentionally tracking how they got there not where they are.
They got there by the path stored in PWD.

I hate that behavior.  But as with most things I was not consulted. :-}

In order to do what you want there are at least three options.  One is
to use the external coreutils version.  The idiom for forcing external
commands is using 'env' for it.

  env pwd

Another is adding the -P option.  This ignores PWD and returns the
physical path.

  pwd -P

And the third (what I do) is to set the shell to always use physical
paths.  Which is how it behaved before they added logical path
tracking in the PWD variable.  I have this in my ~/.bashrc file.

  set -o physical

Therefore I have closed this bug report for the purpose of triage of
the report in the coreutils tracker since this is really about bash
and not coreutils.  However please do reply as discussion may
continue.  We would love to continue the discussion.

Note that the coreutils 'pwd' defalts to -P, --physical unless -L,
--logical is given explicitly.  And that the documentation for the
coreutils pwd is subtly different from the bash version:

    '-L'
    '--logical'
         If the contents of the environment variable 'PWD' provide an
         absolute name of the current directory with no '.' or '..'
         components, but possibly with symbolic links, then output those
         contents.  Otherwise, fall back to default '-P' handling.

    '-P'
    '--physical'
         Print a fully resolved name for the current directory.  That is,
         all components of the printed name will be actual directory
         names—none will be symbolic links.

       If '-L' and '-P' are both given, the last one takes precedence.  If
    neither option is given, then this implementation uses '-P' as the
    default unless the 'POSIXLY_CORRECT' environment variable is set.

       Due to shell aliases and built-in 'pwd' functions, using an unadorned
    'pwd' interactively or in a script may get you different functionality
    than that described here.  Invoke it via 'env' (i.e., 'env pwd ...') to
    avoid interference from the shell.

Hope this helps!

Bob