简体   繁体   中英

Git alias bash function command not found

I added an alias in my ~/.gitconfig :

[alias]
    h = "!git status -s | LC_ALL=C sort -k1 | my_function"

But when I call git h , I get:

> git h
git status -s|LC_ALL=C sort -k1| my_function: my_function: command not found

Now, I know that my function exists in my local shell. It's in my .bashrc and I can see it when I call compgen -A function :

> compgen -A function|grep my_function
my_function
> my_function
(expected output)

Why isn't git recognizing that I've already defined the function in my shell?

When git runs an alias, it actually runs it in a subprocess. If you start the alias with ! , then it is translated into sh -C "$aliasWithoutBang" by the Git processor itself. This means it does not actually run in the same shell with the same variables or functions as the environment calling git.

What seems to be the better approach

h = bash -ic 'git status -s | LC_ALL=C sort -k1 | my_function'

This has two benefits.

  1. It uses an interactive shell ( -i ) and so loads the .bashrc .
  2. It specifically launches into bash .

Thanks to Glenn Jackman for the comments.


How I solved it

I expect there is a better way to do this, but I was able to resolve my problem by changing the alias to:

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

Basically, I forced Git to re-load the environmental variables for this particular alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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