in reply to Getting the shell's version of working directory, without PWD's help

I'm thinking no

I am also thinking no. Once you have performed your cd into sym1 the real directory you are in is dir1 so that's where the script thinks it is (and it's quite right to do so).

An obvious question is why you would want this? I see no benefit.


🦛

Replies are listed 'Best First'.
Re^2: Getting the absolute path of a script, without PWD
by LanX (Sage) on Jul 15, 2021 at 15:40 UTC

      The pwd command and cwd() give different results on my system (bash 4.4.20(1) on Linux).

      % cd /tmp
      % mkdir foo
      % cd foo
      % mkdir dir1
      % ln -s dir1 sym1
      % cd sym1
      
      % echo PWD
      /tmp/foo/sym1
      
      % pwd
      /tmp/foo/sym1
      
      % perl -MCwd=getcwd -E'say getcwd'
      /tmp/foo/dir1
      
      % perl -MCwd=cwd -E'say cwd'
      /tmp/foo/dir1

      But pwd and PWD sometime give different results too.

      UPDATE:The difference is pwd can be instructed to return "logical" and "physical" version, and the logical version still gets information from PWD. Cwd's documentation: "The cwd() is the most natural form for the current architecture. For most systems it is identical to `pwd` (but without the trailing line terminator)." might perhaps be improved by saying: "The cwd() is the most natural form for the current architecture. For most systems it is identical to `pwd -P` (but without the trailing line terminator).

        This seems to be a portability issue.

        cwd()."\n" eq `pwd -L`

        • holds for termux, win10
        • fails for my (old) ubuntu in a VM

        I'd say file a bugreport against Cwd and in the meantime resort to `pwd` or `pwd -L` on systems where it fails.

        update

        after further investigation, it seems that `pwd` inside Perl doesn't default like pwd in bash ... which is unexpected

        lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd` ' /tmp/dir lanx@ubuntu14-large:/tmp/sym$ pwd /tmp/sym lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd -L` ' /tmp/sym lanx@ubuntu14-large:/tmp/sym$

        so strictly speaking, this is not a bug in Cwd, since cwd() returns the same like `pwd` (which does not always -L )

        The question is why is bash's pwd defaulting differently inside Perl?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^2: Getting the absolute path of a script, without PWD
by perlancar (Hermit) on Jul 15, 2021 at 12:02 UTC
    One reason is for logging path and the symlinked version is more meaningful and/or hides unwanted detail. For example I have a device /media/USER/1234-5678 mounted but I also keep /media/USER/MORE-MEANINGFUL-NAME symlinked to it and want to log the filenames inside the symlinked version. Okay not a great example as I should've just relabeled the removable volume in the first place, but that's the only example I can think of right now. There are other cases I've encountered previously.

      Sure - there may be some cases where you might want the symlink name rather than the target. But that's not what you've asked. Your question says "... without PWD". Why impose this arbitrary restriction? If you want what $PWD gives, why not just use it?


      🦛

        Yes, that's just a curiosity. I'm using PWD right now.

      Find the symlinks and build a reverse hash lookup-table.


      Enjoy, Have FUN! H.Merijn
        This will result in a permutation of possible paths. How would you know which path is the correct one?