question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Split-Path -Parent / currently broken on Unix

See original GitHub issue

Steps to reproduce

Split-Path -Parent /

Expected behavior

The empty string (current behavior on Windows), but, ideally, / - see #4131

Actual behavior

An error occurs.

Split-Path : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.

Environment data

PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
SteveL-MSFTcommented, Mar 1, 2018

@PowerShell/powershell-committee reviewed this. Expectation is that scripts using the current Windows PowerShell behavior to parse a file path would need to special case Unix if we have non-symmetric behavior. For example:

$path = "/"; while ($path -ne "") { $path = split-path -parent $path;$path }

This loops forever since the ending state is never hit. To support the same scripts working on Windows and Unix, we need to fix the behavior on Unix so that:

split-path -leaf / => returns / split-path -parent / => returns “”

1reaction
SteveL-MSFTcommented, Feb 12, 2018

I think it’s fine to not have symmetry for the sake of symmetry. On Unix, if the expectation is that the -Leaf and -Parent of / is /, then I think that would be ok and just documented. On Windows, I don’t think we’ll change it as it would be a breaking change w/o clear benefit to warrant the change. Will have @PowerShell/powershell-committee review.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Split-Path "/" -Leaf' returns empty string instead of "/ ...
On Unix, Split-Path / -Parent is currently broken, as you reported in 'Split-Path "/" -Parent' fails on Ubuntu1804/Linux/Wsl #14392, which is a ...
Read more >
How to split path by last slash? - bash
I need to write a bash script that processes one path at a time, splits it at the last slash and then launches...
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 >
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 >
Powershell split-path formatting
This should work regardless of how many sub folders are in the path: $result = $ItemDirectory While (($result -split '\\' | ? {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found