Git 别名找不到 bash 脚本文件,尽管它在 PATH 中

Git alias cannot find bash script file although its in PATH

我想使用 git 别名从 git bash shell 调用 bash 函数,所以我创建了 .git配置看起来像:

[alias]
    jjj = "!f() { source "test_script.sh"; foo $@; }; f;"
    st = status

我的 test_script.sh 在

/c/Users/myname/scripts/

test_script.sh 文件如下所示:

#!/bin/bash 
function foo {
    echo "This is a test script, calling foo!!!";
}

我的路径如下所示:

/c/Users/myname/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/myname/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/Program Files/nodejs:/cmd:/c/Program Files (x86)/Brackets/command:/c/Program Files/Java/jdk1.8.0_111/bin:%APPDATA%/../Local/Android/sdk/platform-tools:/c/Users/myname/scripts:/c/Users/myname/AppData/Roaming:/c/Users/myname/scripts:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Users/myname/scripts

我在 运行ning: git jjj 时得到的是:

f() { source 'test_script.sh' && foo $@; }; f;: line 0: source: test_script.sh: file not found

尽管我的 PATH 环境变量中有我的 test_script.sh 文件的路径,但仍出现此错误。 请注意,当我从任何地方 运行 test_script.sh 成功触发 test_script.sh

问:我该如何解决这个问题,或者更好地调试这个问题?这让我很想吃香蕉三明治

我的设置是:

如果您不想使用文件的绝对路径(因为您希望别名可在不同的上下文中重复使用),则需要检查您的 PATH 实际是什么。

将您的别名替换为 echo $PATH; shopt sourcepath

这将检查您的 PATH 以及是否允许 source 使用它(参见 man bash:如果 shoptsourcepath 选项内置命令已关闭,PATH 未被搜索)

如评论中所示,PATH 未导出(因此别名生成的子进程未继承所述变量)

.bashrc 中添加它 (export PATH=...) 是不够的。

查看“Bash doesn't read .bashrc unless manually started" and bashrc not loaded in /bin/bash shell,您需要 %USERPROFILE%\.bash_profile 采购 .bashrc