别名没有任何影响

Aliasing doesn't have any effect

我有一个 Jenkins 管道脚本。

在其中,这个有效:

sh("/my/path/to/git status")

但是,如果我尝试:

sh("alias git='/my/path/to/git' && git status")

sh("alias git='/my/path/to/git'")
sh("git status")

这些不起作用:script.sh: line 2: git: command not found

我想让第二段和第三段代码也能正常工作。我该怎么做?

这些行

sh("alias git='/my/path/to/git'")
sh("git status")

创建 两个 子shell。第一个创建您的别名,然后立即退出。第二个启动时不知道前面的 shell 或其别名。

上一版本

sh("alias git='/my/path/to/git' && git status")

在本地交互 shell 中也不起作用,即使 && 被替换为 ; - 显然别名直到结束才生效当前命令列表。

如果必须使用别名,则应将其添加到启动 [=33] 时来源的任何 shell 文件(.bashrc.profile 等) =].但是请注意,除非您 shopt -s expand_aliases.

否则别名可能无论如何都不会在非交互式 shell 中扩展

否则,通常的解决方案是将 /my/path/to 添加到您的 $PATH

不允许连续sh调用保持状态(包括环境变量)。

在您的项目中创建一个脚本并在单个 sh 指令中调用它,或者使用:

sh """
  alias git='/my/path/to/git'
  git status
"""