bash 带参数的 ZSH 别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34340575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ZSH alias with parameter
提问by albttx
I am trying to make an alias with parameter for my simple git add/commit/push.
我正在尝试为我的简单 git add/commit/push 使用参数创建别名。
I've seen Function could be used as alias so i try but i didn't make it ..
我已经看到 Function 可以用作别名,所以我尝试但我没有成功..
before i had:
在我之前:
alias gitall="git add . ; git commit -m 'update' ; git push"
But i want to be able to modify my commits :
但我希望能够修改我的提交:
function gitall() {
"git add ."
if [ != ""]
"git commit -m "
else
"git commit -m 'update'"
fi
"git push"
}
(i know it's a terrible git practice)
(我知道这是一个糟糕的 git 练习)
回答by Kevin
You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the []
.
你不能用参数*来创建别名,它必须是一个函数。您的函数很接近,您只需要引用某些参数而不是整个命令,并在[]
.
gitall() {
git add .
if [ "" != "" ] # or better, if [ -n "" ]
then
git commit -m ""
else
git commit -m update
fi
git push
}
*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.
*: 大多数 shell 不允许在别名中使用参数,我相信 csh 和衍生工具允许,但无论如何你都不应该使用它们。
回答by joelpt
If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:
如果您出于某种原因确实需要使用带有参数的别名,您可以通过在别名中嵌入一个函数并立即执行它来破解它:
alias example='f() { echo Your arg was . };f'
I see this approach used a lot in .gitconfig aliases.
我看到这种方法在 .gitconfig 别名中使用了很多。
回答by Hasan Abdullah
I used this function in .zshrc file:
我在 .zshrc 文件中使用了这个函数:
function gitall() {
git add .
if [ "" != "" ]
then
git commit -m ""
else
git commit -m update # default commit message is `update`
fi # closing statement of if-else block
git push origin HEAD
}
Here git push origin HEAD
is responsible to push your current branch on remote.
这里git push origin HEAD
负责将您当前的分支推送到远程。
From command prompt run this command: gitall "commit message goes here"
从命令提示符运行此命令: gitall "commit message goes here"
If we just run gitall
without any commit message then the commit message will be update
as the function said.
如果我们只是在gitall
没有任何提交消息的情况下运行,那么提交消息将update
如函数所述。
回答by Alberto Zaccagni
"git add ."
and the other commands between "
are just strings for bash, remove the "
s.
"git add ."
之间的其他命令"
只是bash的字符串,删除"
s。
You might want to use [ -n "$1" ]
instead in your if body.
您可能想[ -n "$1" ]
在 if 正文中使用它。