[英]How to launch a Bash function using Git alias
I want to use a Git alias in ~/.gitconfig so that it calls a bash function, if it is defined, otherwise call the regular git checkout
. 我想在〜/ .gitconfig中使用Git别名,以便它调用bash函数(如果已定义),否则调用常规git checkout
。
This is what I have devised: 这就是我设计的:
cat ~/.gitconfig
...
[alias]
...
co = !(compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout ) || git checkout
The problem is that Git uses /bin/sh (which happens to be dash
in my case) and it barfs on compgen
since it is a bash
builtin. 问题是Git使用/ bin / sh(碰巧在我的情况下是dash
)并且它在compgen上是compgen
因为它是bash
内置的。
Any way making sure that Git uses bash
to execute this command? 确保Git使用bash
执行此命令的任何方式?
Note that vxzExecuteGitCheckout()
is defined in a file which is not included in ~/.bashrc, yet. 请注意, vxzExecuteGitCheckout()
是在〜/ .bashrc中未包含的文件中定义的。
Another question is, if I were to use git co -b param1 param2 ...
, would the above alias definition pass on the -b param1 param2
to git checkout
if this Bash function is not found? 另一个问题是,如果我使用git co -b param1 param2 ...
,如果找不到这个Bash函数,上面的别名定义是否会将-b param1 param2
传递给git checkout
?
use bash
explicitely: 明确使用bash
:
co = !bash -c '( compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout ) || git checkout' -
another possibility would be to write a shell script with the correct shebang line #!/bin/bash
and call that script for alias.co
(see the question How to embed bash script directly inside a git alias ) 另一种可能性是使用正确的shebang行编写一个shell脚本#!/bin/bash
并为alias.co
调用该脚本(请参阅如何在git别名中直接嵌入bash脚本的问题)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.