如何在git别名中使用bash函数? [英] How to use a bash function in a git alias?

查看:148
本文介绍了如何在git别名中使用bash函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在git别名中使用bash函数。因此,我将其添加到了 .bashrc

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

到我的 .gitconfig

[alias]
    fn = !fn

但是随后 git fn 会产生错误:

fatal: cannot run fn: No such file or directory
fatal: While expanding alias 'fn': 'fn': No such file or directory

这是在git别名定义中使用bash函数的正确方法吗?

Is this a correct way to use a bash function in a git alias definition?

推荐答案

那是因为git使用 / bin / sh (所以您的 .bashrc 未获得。)

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

您可以按照这个答案

问题是git命令启动的bash shell没有加载您的 .profile (负责包含 .bashrc 的人)。

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'

文件为 .my_functions

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

您甚至可以获取 .my_functions 放入您的 .bashrc 中,如果您希望这些功能可以从常规shell中获得。

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

这篇关于如何在git别名中使用bash函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆