Comments (2)
Problem is that when you don't expand the ~
when calling native command with variable arguments, ~
stops working for PowerShell functions which internally call native commands, which might be surprising. As much as I despise the behavior, I see the rationale for the current design.
from powershell.
@MatejKafka, there's an unresolvable tension when different worlds collide, and I understand that there's no ideal solution here:
Introducing POSIX-like shell expansions invariably introduces a concept that is foreign to PowerShell: that whether characters such as ~
and *
are individually escaped / inside quoted strings or not matters.
To PowerShell itself, these distinctions do not matter, given that ~
and *
are not metacharacters at the shell level - they are passed as-is to the target PowerShell command, which may or may not choose to assign special meaning to them - such as an initial ~
referring to the underlying provider's home location in provider cmdlets, and *
representing a wildcard metacharacter in arguments passed to parameters that happen to support wildcard expressions.
If PowerShell were to strictly adhere to this model, neither ls ~
nor ls *.txt
would work on Unix-like platforms, given that the /bin/ls
utility neither knows what ~
refers to nor can locate a file-system item literally named *.txt
.
For a more seamless integration of external utilities (aka native programs) on Unix, PowerShell (Core) has from the beginning decided to emulate these specific shell expansion (tilde expansion and pathname expansion (globbing)), which invariably entailed introducing the aforementioned foreign concept into PowerShell; without it, you couldn't distinguish between - say - using *.txt
to mean "all files in the current directory with extension .txt
" and verbatim *.txt
These concepts are more foreign to Windows users:
-
With respect to pathname expansion (globbing), the issue doesn't arise: this emulation can never be introduced on Windows, as it would break backward compatibility: external utilities on Windows that rely on globbing must perform it themselves, and PowerShell suddenly doing it for them and passing multiple arguments that result from globbing, say,
*.txt
could break such utilities. -
However, tilde expansion is less foreign:
~
does have established semantics in PowerShell (but not Windows in general, and not incmd.exe
): it refers to a provider's home location, which notably doesn't necessarily mean the file-system provider's home location, but that's a separate issue: in the context of external utilities, it only ever makes sense to interpert~
as the file-system's home location, i.e. as$HOME
.- As such, extending these semantics to external utilities is a more natural extension - even though the escaping/quoting part isn't.
- The awkwardness comes from needing to understand the latter part, which includes knowing that
~
therefore cannot be used in variable values, because PowerShell inherently doesn't allow defining string variables without quoting.- By contrast, in POSIX-like shells, you can use unquoted values in variable assignments, and therefore take advantage of the usual shell expansions; e.g.,
p=~
is equivalent top="$HOME"
- You cannot do the same in PowerShell, and therefore need to know that
~
in argument mode, when calling external utilities, is the same as$HOME
, so in variable assignments$p = $HOME
is required.
- By contrast, in POSIX-like shells, you can use unquoted values in variable assignments, and therefore take advantage of the usual shell expansions; e.g.,
from powershell.
Related Issues (20)
- Does PowerShell support the real "Stream" response? HOT 5
- v7.4.2 not in Ubuntu 22.04 Linux package repository for Microsoft products HOT 3
- `Finally` block isn't executed upon Ctrl+C HOT 36
- Make `ConvertFrom-StringData` more error tolerant HOT 9
- Add-Type ReferencedAssemblies only works with full file paths now? HOT 1
- .Net 8 in PowerShell 7 System.Private.CoreLib fails to register. HOT 41
- `ConvertFrom-StringData` mistakenly emits a statement-terminating error when one of its input strings isn't well-formed HOT 2
- Redirection operator `>` should not be hardcoded to files HOT 6
- Update from System.Data.SqlClient to Microsoft.Data.SqlClient HOT 3
- .NET App using Powershell.SDK fails to run, when building as SingeFile HOT 2
- Add a command which gets the System Lockdown mode information HOT 2
- Overriding .NETs ToString() method can cause unexpected behaviour when using Transcripts HOT 1
- `Compare-Object` doesn't compare custom value types (`structs`) (that don't implement `IComparable`) correctly HOT 4
- Invoke-WebRequest needs SupportsShouldProcess
- using `Get-AppxPackage` when `SYSTEMDRIVE` env var is not set creates a (literal) `%SystemDrive%/…` directory HOT 7
- Get-Help shows incorrect spacing and unwanted prefixes for first line of .EXAMPLE text HOT 6
- Argument with colon in it is incorrectly handled if not quoted HOT 12
- Get-AuthenticodeSignature needs to use embedded signatures HOT 5
- Change JsonConvert.DefaultSettings cause ExchangeOnlineModule to fail HOT 3
- Quirk behavior auto conversion string to integer (based on Get-UICulture instead of Get-Culture) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell.