[英]Git alias bash function command not found
我在~/.gitconfig
添加了一个别名:
[alias]
h = "!git status -s | LC_ALL=C sort -k1 | my_function"
但是当我打电话给git h
,我得到:
> git h
git status -s|LC_ALL=C sort -k1| my_function: my_function: command not found
现在,我知道我的函数存在于我的本地 shell 中。 它在我的.bashrc
中,当我调用compgen -A function
时可以看到它:
> compgen -A function|grep my_function
my_function
> my_function
(expected output)
为什么git
没有识别出我已经在我的 shell 中定义了函数?
当git
运行一个别名时,它实际上是在一个子进程中运行它。 如果您使用!
,然后它被 Git 处理器本身翻译成sh -C "$aliasWithoutBang"
。 这意味着它实际上并不在与调用 git 的环境具有相同变量或函数的同一个 shell 中运行。
h = bash -ic 'git status -s | LC_ALL=C sort -k1 | my_function'
这有两个好处。
-i
),因此加载.bashrc
。bash
。感谢格伦杰克曼的评论。
我希望有更好的方法来执行此操作,但我能够通过将别名更改为以下方式来解决我的问题:
h = "!. ~/.bashrc && git status -s | LC_ALL=C sort -k1 | my_function"
基本上,我强制 Git 为这个特定别名重新加载环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.