我想添加一个复杂的 git 别名,它会在执行命令时回显消息。我想给一些消息着色(红色表示错误等)。
[alias]
test = !"f() { echo "\033[31mHello\033[0m World"; }; f"
但是,当我执行别名时出现错误:
bad config line X in file .gitconfig`
在终端中运行相同的命令 echo "\033[31mHello\033[0m World"
着色就好了。
最佳答案
反斜杠必须转义。来自 git-config 文档...
Inside double quotes, double quote " and backslash \ characters must be escaped: use \" for " and \ for .
The following escape sequences (beside \" and \) are recognized: \n for newline character (NL), \t for horizontal tabulation (HT, TAB) and \b for backspace (BS). Other char escape sequences (including octal escape sequences) are invalid.
这会起作用。
test = !"f() { echo \"\\033[31mHello\\033[0m World\"; }; f"
但是如果您的别名太复杂以至于您需要定义可能会变成一团糟的函数。我建议将这些函数放在它们自己的文件中并获取它。
test = !"source ~/.gitfuncs; f"
$ cat ~/.gitfuncs
f() { echo "\033[31mHello\033[0m World"; };
关于函数中的 Git 别名输出着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403640/