繁体   English   中英

找不到 Git 别名 bash 函数命令

[英]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'

这有两个好处。

  1. 它使用交互式 shell ( -i ),因此加载.bashrc
  2. 它专门启动到bash

感谢格伦杰克曼的评论。


我是怎么解决的

我希望有更好的方法来执行此操作,但我能够通过将别名更改为以下方式来解决我的问题:

h = "!. ~/.bashrc && git status -s | LC_ALL=C sort -k1 | my_function"

基本上,我强制 Git 为这个特定别名重新加载环境变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM