@@ -431,11 +431,13 @@ function Remove-FromEnvPath {
431431function Sync-PathFromBash {
432432 <#
433433 . SYNOPSIS
434- 同步 Bash 登录 Shell 的 PATH 到当前 PowerShell 会话。
434+ 同步 Bash 的 PATH(默认非登录,从 .bashrc) 到当前 PowerShell 会话。
435435 . DESCRIPTION
436- 在 Linux/macOS 上,通过调用 `bash -l` 获取完整配置后的 PATH,
437- 比较当前 PowerShell 的 PATH,追加缺失路径。支持前置/后置策略,
438- 可选过滤不存在的目录,并返回结构化结果用于编程处理。
436+ 默认采用非登录模式(不加载 /etc/profile),通过 `bash -ci` 读取由 `.bashrc` 配置的 PATH,
437+ 并与 PowerShell 的 PATH 对比追加缺失项。可通过 `-Login` 开启登录模式(`bash -lc`)。
438+ 支持前置/后置策略、目录有效性过滤、结构化返回对象与安全预览。
439+ . PARAMETER Login
440+ 启用登录模式(`bash -lc`),获取完整登录环境 PATH。
439441 . PARAMETER Prepend
440442 将缺失路径前置到 PATH 开头,使 Bash 的路径优先生效。
441443 . PARAMETER IncludeNonexistent
@@ -444,33 +446,48 @@ function Sync-PathFromBash {
444446 返回包含统计与结果的 `PSCustomObject`(默认 true)。
445447 . EXAMPLE
446448 Sync-PathFromBash -WhatIf -Verbose
447- 预览将要变更的 PATH,不实际更改,并显示详细日志。
449+ 以非登录模式预览将要变更的 PATH,不实际更改,并显示详细日志。
448450 . EXAMPLE
449- Sync-PathFromBash -Prepend -Verbose
450- 将 Bash 中缺失的目录前置到 PATH,适合优先使用 Bash 环境 。
451+ Sync-PathFromBash -Login - Prepend -Verbose
452+ 以登录模式将 Bash 中缺失的目录前置到 PATH,适合优先使用完整登录环境 。
451453 #>
452454 [CmdletBinding (SupportsShouldProcess = $true , ConfirmImpact = ' Low' )]
453455 param (
456+ [switch ]$Login ,
454457 [switch ]$Prepend ,
455- [switch ]$Append ,
456458 [switch ]$IncludeNonexistent ,
457459 [bool ]$ReturnObject = $true
458460 )
459461
460462 try {
461463 $start = [DateTime ]::UtcNow
462464 Write-Information " 正在从 Bash 登录 Shell 中获取 PATH..."
463- if ($Prepend -and $Append ) { throw " 同时指定 Prepend 与 Append,不支持;请二选一" }
464-
465- $bashPathOutput = bash - l - c ' echo $PATH'
466- $source = ' bash-login'
467- if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($bashPathOutput )) {
468- Write-Warning " 无法从 Bash 登录 Shell 获取 PATH,尝试回退方案。"
469- $bashPathOutput = bash - c ' source /etc/profile >/dev/null 2>&1; echo $PATH'
470- $source = ' bash-profile-fallback'
465+ $bashPathOutput = ' '
466+ $source = ' '
467+ if ($Login ) {
468+ $bashPathOutput = bash - lc ' echo $PATH'
469+ $source = ' bash-login'
471470 if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($bashPathOutput )) {
472- Write-Warning " 无法从 Bash 获取 PATH。Bash 可能未安装或存在配置错误。"
473- return
471+ Write-Warning " 无法从 Bash 登录 Shell 获取 PATH,尝试回退 /etc/profile。"
472+ $bashPathOutput = bash - c ' source /etc/profile >/dev/null 2>&1; echo $PATH'
473+ $source = ' bash-profile-fallback'
474+ if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($bashPathOutput )) {
475+ Write-Warning " 无法从 Bash 获取 PATH。Bash 可能未安装或存在配置错误。"
476+ return
477+ }
478+ }
479+ }
480+ else {
481+ $bashPathOutput = bash - ci ' echo $PATH'
482+ $source = ' bash-nologin-bashrc'
483+ if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($bashPathOutput )) {
484+ Write-Warning " 非登录模式获取 PATH 失败,尝试显式加载 ~/.bashrc。"
485+ $bashPathOutput = bash -- noprofile -- norc - c ' source ~/.bashrc 2>/dev/null; echo $PATH'
486+ $source = ' bash-nologin-bashrc-fallback'
487+ if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($bashPathOutput )) {
488+ Write-Warning " 无法从非登录模式获取 PATH。请检查 Bash 安装或 .bashrc 配置。"
489+ return
490+ }
474491 }
475492 }
476493
0 commit comments