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
and to my .gitconfig
:
[alias]
fn = !fn
But then git fn
produces an error:
fatal: cannot run fn: No such file or directory
fatal: While expanding alias 'fn': 'fn': No such file or directory
Is this a correct way to use a bash function in a git alias definition?
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)....properly emits:
The relevant entry in
.gitconfig
:I don't know why, but if I just put a space before the function name in
.gitconfig
, it works without a problem and outputs the message:It might be a bug in git, or I'm missing something in the documentation.
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:
With the file
.my_functions
like this:You can even source
.my_functions
into your.bashrc
if you want the functions to be available from regular shells.