in reply to Re^2: Getting the absolute path of a script, without PWD
in thread Getting the shell's version of working directory, without PWD's help

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).

  • Comment on Re^3: Getting the absolute path of a script, without PWD

Replies are listed 'Best First'.
Re^4: Getting the absolute path of a script, without PWD (bug?)(update)
by LanX (Sage) on Jul 16, 2021 at 07:21 UTC
    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

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

      From qx

      Similarly to system, if the string contains no shell metacharacters then it will executed directly.

      Not sure what is included into "shell metacharacters" but a simple semicolon seems to suffice to force execution via the shell ...

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

      update

      simplified code

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

        You're 1 minute faster :-)
      after further investigation, it seems that `pwd` inside Perl doesn't default like pwd in bash ... which is unexpected

      The answer is perhaps this in perlop for the qx//:

      if the string contains no shell metacharacters then it will executed directly.

      So although in general qx// uses the shell, if we say `pwd` then perl will execute /bin/pwd, which defaults to -P. If we say `bash -c pwd` then bash will execute its shell builtin version of pwd, which defaults to -L.

        > it seems that `pwd` inside Perl doesn't default like pwd in bash

        Note that qx// doesn't call bash but sh, which might be a different shell (/bin/dash on Ubuntu, for example).

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]