Powershell: Unix utilities expect $env:PWD to reflect the current directory, as per POSIX

Created on 27 Jul 2018  路  6Comments  路  Source: PowerShell/PowerShell

POSIX mandates the presence of an _environment_ variable named PWD that should "represent an absolute pathname of the current working directory."

That is, POSIX-like shells keep the value of this env. variable in sync with their working dir. so that (non-shell) processes created from the shell can rely on it reflecting the shell's notion of the current dir.

PowerShell currently doesn't set this environment variable (it only sets its internal automatic $PWD variable, which other processes cannot see), which can have unintended consequences when invoking Unix utilities that rely on it.

This problem is conceptually related to #3428, for which there is no good solution due to its in-process nature.

In the case at hand, however, since a _child process_ must be created to invoke an external program, it should be possible to add a PWD environment variable reflecting PowerShell's current filesystem location to the environment block of the new process.

Steps to reproduce (on macOS or Linux)

# Switch to an arbitrary location and make the Perl process display the value of $env:PWD
Set-Location /tmp; perl -le 'print $ENV{PWD}'  

Expected behavior

/tmp

That is, the external utility's process should see PowerShell's current filesystem location in its PWD environment variable.

(Note that using another shell (e.g., bash) for this test wouldn't be a valid test, because POSIX-like shells set the PWD environment variable themselves.)

Actual behavior

/home/jdoe     # e.g.

Even with PowerShell configured as the user's default shell, $env:PWD is still set at the time the PowerShell session starts, and seemingly always reflects the user's home dir.

Environment data

PowerShell Core v6.1.0-preview.4 on macOS 10.13.6
PowerShell Core v6.1.0-preview.4 on Ubuntu 16.04.4 LTS
Issue-Enhancement WG-Engine

Most helpful comment

It seems we could implement this with something like this property

All 6 comments

Is there a goal to make PowerShell POSIX compliant?

@thezim:

It's not about POSIX compliance per se - it's just that POSIX-decreed functionality is, by and large, the common denominator across macOS and Linux - see https://github.com/PowerShell/PowerShell/issues/6518#issuecomment-376914142

If PowerShell wants to be a good cross-platform citizen and aspires to be the "universal shell" (which I hope it does), it must not contravene fundamental expectations of any platform's native utilities.

Utilities (command-line programs) on Unix-like platforms may rely on environment variable ENV ($env:PWD, in PowerShell terms) reflecting the notion of the calling shell's current directory.

PowerShell not respecting that can lead to subtle failures.

If you want a specific example:

After switching my default shell to PowerShell on macOS, my (standardized across projects) Makefiles started to break, because they try to add project-relative directories to $env:PATH, based on $env:PWD.

I believe it will be easy-to-fix and good enhancement.

Just got bitten by this after changing my login shell to pwsh 7.0.0-preview.3. I expect lots of tools have code like this: https://github.com/chef/chef/blob/39cf0beb9ccc0fa0f60fa9d52b3c0a2bccc6c0f1/chef-config/lib/chef-config/workstation_config_loader.rb#L141-L149 and more people will be using pwsh as their login shell now...

My particular issue can be solved by setting $env:PWD to an empty string, but I expect that'll break something else I haven't tried yet.

GitHub
A systems integration framework, built to bring the benefits of configuration management to your entire infrastructure. - chef/chef

It seems we could implement this with something like this property

Was this page helpful?
0 / 5 - 0 ratings