'Split-Path "/" -Leaf' returns empty string instead of "/" on Ubuntu1804/Linux/Wsl
See original GitHub issueSteps to reproduce
On WSL Ubuntu 1804 or probably any other linux distro
Split-Path "/" -Leaf
Expected behavior
return a slash
/
Actual behavior
returns an empty string
Environment data
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Linux 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Split-Path doesn't like colons in the Leaf
It's a path stored as a string in an XML document. ... no problem returning the leaf value, but the parent path is...
Read more >How to Split Paths with the PowerShell Split-Path Cmdlet
Learn how to split and return different elements of a path using the PowerShell Split-Path cmdlet in this tutorial.
Read more >Split-Path - PowerShell
The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Don’t get me wrong. I agree to you on how this should be handled. I was just giving a different point of view. Doesn’t mean it was the proposed way to fix it.
Just a terminology quibble:
On Window, they are rooted (
[IO.Path]::IsPathRooted()
returns$true
), but not absolute ([IO.Path]::IsPathFullyQualified()
returns$false
)On Unix, they are both rooted and absolute.
Using
Join-Path
andSplit-Path
is good practice:Join-Path
avoids theC:\\foo
problem (thoughSplit-Path
doesn’t correct it).However, another reason not to treat
''
as the root directory is thatJoin-Path '' foo
simply fails.Also, as stated, if you made
Split-Path /foo -Parent
return''
(even though on Unix it currently does, but that’s a variation of the bug at hand), you couldn’t distinguish it fromSplit-Path foo -Parent
- at least not without introducing a serious breaking change (such as to return.
in the latter case).Similarly, for consistency, you’d have to then allow
Get-ChildItem ''
to list the root directory, whereas it currently lists the current one.In short: It is generally problematic to refer to a specific something (the root dir.) with nothing (the empty string), and using
\
//
as the name of the root dir. is the best compromise.As for how
Split-Path C:\ -<switch>
should be handled:Split-Path C:\ -Leaf
on Windows andSplit-Path / -Leaf
on Unix should return the name of the root dir, and therefore\
an/
, respectivelyOn Windows, it currently returns
C:\
, which strikes me as incorrect - but fixing it would be a breaking change.On Unix - which is the bug at hand - it returns
''
(which by the (to me ill-advised) logic of considering''
the root dir.'s name would actually be correct).Split-Path C:\ -Parent
on Windows andSplit-Path / -Parent
on Unix are tricky:The root directory by definition has no parent, and
[IO.Path]::GetDirectoryName()
indicates this by returning$null
- as opposed to the''
returned for something like[IO.Path]::GetDirectoryName('foo')
This is too subtle a distinction for PowerShell, where
[string]
values usually do not contain$null
:On Windows ,
Split-Path C:\ -Parent
currently returns''
- changing that would be a breaking change.On Unix,
Split-Path / -Parent
is currently broken, as you reported in #14392, which is a duplicate of #10092; for consistency, absent a breaking change, it should also return''
.The upshot is that if you call
Split-Path -Parent $path
on an unknown path$path
and get''
back, you’ll need further investigation ([IO.Path]::IsPathRooted()
) to infer whether$path
refers to a root dir. (with or without a drive spec.) or is a mere file or directory name such asfoo
.Split-Path -IsAbsolute
isn’t suitable for this distinction, becauseSplit-Path \ -IsAbsolute
is$false
on Windows.Test-Path
that should have this switch - it never made sense onSplit-Path
. Better yet,Test-Path
should support an enumeration-based-PathForm
/-Form
parameter that allows testing for specific path forms (absolute, relative, rooted, UNC) - see #14402