简体   繁体   中英

How to pass arguments to Unix-like commands (via MinGW) in PowerShell

I'm using MinGW on my Windows machine, and when I use it with the command-prompt , things work as expected:

  • ls lists the visible files
  • ls -a lists all files, etc

But, when I fire up Powershell, it keeps throwing errors when I pass arguments to my commands as so:

D:\>ls -al .

Get-ChildItem : A parameter cannot be found that matches parameter name 'al'.
At line:1 char:4
+ ls -al .
+    ~~~
  + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
  + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Powershell.Commands.GetChildItemCommand

What can I do to use all my MinGW/msys commands properly via Powershell?

ls is an alias in Powershell for the get-childitem command. If you want to run an external command called ls you will need to force Powershell to ignore its own ls command.

You could try running ls.exe , or specify the path to the MinGW command, or undefine the Powershell alias. Or learn to use the options for Powershell's own ls command instead.

The nearest Powershell equivalent to ls -al would be ls -Force This will display all hidden and system files and Powershell's default output for ls looks somewhat like the ls -l format anyway.

You can check which command will be run using Powershell's get-command which does a similar job to which (or use gcm as shorthand for less typing:

PS C:\Users\IEUser> get-command ls.exe

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     ls.exe                                             0.0.0.0    C:\Program Files\OpenSSH\bin\ls.exe


PS C:\Users\IEUser> get-command ls

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem

PS C:\Users\IEUser> gcm ls

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem


PS C:\Users\IEUser> gcm ls.exe

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     ls.exe                                             0.0.0.0    C:\Program Files\OpenSSH\bin\ls.exe

and if you don't want to learn a new command:

PS C:\Users\IEUser> set-alias which gcm
PS C:\Users\IEUser> which ls

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM