What's the difference between "$pwd" and "./"?
Asked Answered
G

2

5

As far as I can see using $pwd and using ./ in PowerShell gives the same result.

Are they the same or is there a difference?

Gloomy answered 22/2, 2018 at 8:41 Comment(0)
C
10

$pwd is an automatic variable. Its name stands for "present working directory" and it should always contain the current working path.

You can use just "." or ".\" as a path parameter to represent the current location, but you can't set a variable to .\ and have it then contain the current path, so in this regard they are different.

As an example if you were writing some script logic that needed to check the current working directory you would need to use $pwd vs .\. For example:

if ($pwd -eq 'c:\some\path') { }

Would work. However:

if (. -eq 'c:\some\path') { }

Would not.

Caret answered 22/2, 2018 at 8:46 Comment(0)
E
5

$PWD and Get-Location (alias: pwd) give you an absolute path, . is a relative path. This can be important when storing a path to re-use later (e.g. in a different location), where . will always be relative to the current location.

Evoy answered 22/2, 2018 at 8:46 Comment(4)
While I understand what you mean in my experiments "$pwd" and "./" give the same output.Gloomy
Those are two very different strings; I cannot fathom how they can result in the same output.Evoy
Well, yes, for Resolve-Path the result is the same. But you chose to omit that tidbit from your comment ;-) I just noticed that Resolve-Path doesn't work like I thought it would anyway; I omitted that part again.Evoy
Oh... sorry. I just don't have enough experience with more complex scripts and for what I know the end result is like the screenshot. But if you say they are different - I believe it.Gloomy

© 2022 - 2024 — McMap. All rights reserved.