Powershell中的CDPATH功能?

Ten*_*ith 6 bash powershell

有人在Powershell中实现了bash的'cdpath'的等效行为吗?

man*_*lds 6

之前不知道CDPATH.很高兴知道.我为Powershell掀起了以下内容:

function cd2 {
    param($path)
    if(-not $path){return;}

    if((test-path $path) -or (-not $env:CDPATH)){
        Set-Location $path
        return
    }
    $cdpath = $env:CDPATH.split(";") | % { $ExecutionContext.InvokeCommand.ExpandString($_) }
    $npath = ""
    foreach($p in $cdpath){
        $tpath = join-path $p $path
        if(test-path $tpath){$npath = $tpath; break;}
    }
    if($npath){
        #write-host -fore yellow "Using CDPATH"
        Set-Location $npath
        return
    }

    set-location $path

}
Run Code Online (Sandbox Code Playgroud)

它不会是完美的,但是以预期的方式工作.我猜你可以扩展它.将其添加到您的个人资料.如果需要,还可以添加如下别名:

set-alias -Name cd -value cd2 -Option AllScope
Run Code Online (Sandbox Code Playgroud)