How to use a bash function in a git alias?

前端 未结 3 1546
醉话见心
醉话见心 2020-12-21 01:59

I want to use a bash function in a git alias. So I added this to my .bashrc:

fn() {
    echo \"Hello, world!\"
}
export -f fn

3条回答
  •  误落风尘
    2020-12-21 02:45

    Thats because git uses /bin/sh (so your .bashrc is not sourced).

    You can call bash in a git alias as specified in this answer.

    The thing is the bash shell started by the git command is not loading your .profile (which is the one responsible for including the .bashrc).

    There may be other ways to do it but you can work around by doing:

    [alias]
        fn = !bash -c 'source $HOME/.my_functions && fn'
    

    With the file .my_functions like this:

    #!/bin/bash
    fn() {
        echo "Hello, world!"
    }
    

    You can even source .my_functions into your .bashrc if you want the functions to be available from regular shells.

提交回复
热议问题