在bash上的`which`的cmd / powershell相当于什么?

我想知道哪个版本的CMD shell使用的可执行文件。 在任何unix shell中,我都会使用which来找到它。

在其中一个Windows shell中是否有等效的命令?


多种。

  • where是直接的等价物:

    C:UsersJoey>where cmd
    C:WindowsSystem32cmd.exe
    

    请注意,在PowerShell中where本身是Where-Object的别名,因此您需要在PowerShell中使用where.exe

  • cmd您还可以for

    C:UsersJoey>for %x in (powershell.exe) do @echo %~$PATH:x
    C:WindowsSystem32WindowsPowerShellv1.0powershell.exe
    
  • 在PowerShell中,您有Get-Command及其别名gcm ,如果您传递参数(但也适用于PowerShell中的别名,cmdlet和函数),它们的作用相同:

    PS C:UsersJoey> Get-Command where
    
    CommandType     Name          Definition
    -----------     ----          ----------
    Alias           where         Where-Object
    Application     where.exe     C:Windowssystem32where.exe
    

    第一个返回的命令是将要执行的命令。


  • WHERE命令与unix不完全相同which因为它列出了当前目录或PATH中找到的所有匹配文件。 正如乔伊所说,列出的第一个是执行的那个。 创建批处理脚本很简单,只会返回找到的第一个脚本。

    @echo off
    for /f "delims=" %%F in ('where %1') do (
      echo %%F
      exit /b
    )
    

    但是WHERE比较慢。

    下面是一个WHICH.BAT脚本,速度更快,功能更强大。 它使用广泛的延迟扩展切换,因为:1)如果没有引号的特殊字符,扩展%PATH%是不可靠的。 2)当延迟扩展被启用时扩展FOR变量损坏包含!

    ::WHICH.BAT  CommandName  [ReturnVar]
    ::
    ::  Determines the full path of the file that would execute if
    ::  CommandName were executed.
    ::
    ::  The result is stored in variable ReturnVar, or else it is
    ::  echoed to stdout if ReturnVar is not specified.
    ::
    ::  If no file is found, then an error message is echoed to stderr.
    ::
    ::  The ERRORLEVEL is set to one of the following values
    ::    0 - Success: A matching file was found
    ::    1 - CommandName is an internal command
    ::    2 - No file was found and CommandName is not an internal command
    ::    3 - Improper syntax - no CommandName specified
    ::
    @echo off
    setlocal disableDelayedExpansion
    
    set "file=%~1"
    setlocal enableDelayedExpansion
    
    if not defined file (
      >&2 echo Syntax error: No CommandName specified
      exit /b 3
    )
    
    
    :: test for internal command
    echo(!file!|findstr /i "[^abcdefghijklmnopqrstuvwxyz]" >nul || (
      set "empty=!temp!emptyFolder"
      md "!empty!" 2>nul
      del /q "!empty!*" 2>nul >nul
      setlocal
      pushd "!empty!"
      set path=
      (call )
      !file! /? >nul 2>nul
      if not errorlevel 9009 (
        >&2 echo "!file!" is an internal command
        popd
        exit /b 1
      )
      popd
      endlocal
    )
    
    
    :: test for external command
    set "noExt="
    if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt="";
    set "modpath=.;!PATH!"
    @for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do (
      setlocal disableDelayedExpansion
      if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F" (
        endlocal & endlocal & endlocal
        if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F"
        exit /b 0
      )
      endlocal
    )
    endlocal
    
    
    >&2 echo "%~1" is not a valid command
    exit /b 2
    

    UPDATE

    我不得不大幅修改上面的脚本,因为如果碰巧在PATH中存在一个具有相同根名称的exe文件,它会错误地将内部命令列为外部命令。

    链接地址: http://www.djcxy.com/p/30263.html

    上一篇: Whats the cmd/powershell equivalent of `which` on bash?

    下一篇: Where does Anaconda Python install on Windows?