How to use a bash function in a git alias?

前端 未结 3 1536
醉话见心
醉话见心 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:41

    If you want to be 100% certain that exported functions will be honored, ensure that the shell being called is bash, not /bin/sh (which will not honor them if it's implemented by ash or dash).

    fn() { echo "hello, world"; }
    export -f fn
    git config --global alias.fn $'!bash -c \'fn "$@"\' _'
    git fn
    

    ...properly emits:

    hello, world
    

    The relevant entry in .gitconfig:

    [alias]
        fn = !bash -c 'fn \"$@\"'
    

提交回复
热议问题