带参数的 ZSH 别名
ZSH alias with parameter
我正在尝试为我的简单 git add/commit/push.
创建一个带有参数的别名
我看到一个函数可以用作别名,所以我试了但没成功。
之前我有:
alias gitall="git add . ; git commit -m 'update' ; git push"
但我希望能够修改我的提交:
function gitall() {
"git add ."
if [ != ""]
"git commit -m "
else
"git commit -m 'update'"
fi
"git push"
}
"git add ."
和 "
之间的其他命令只是 bash 的字符串,删除 "
s。
您可能想在 if 正文中使用 [ -n "" ]
。
你不能创建带参数的别名*,它必须是一个函数。您的功能很接近,您只需要引用某些参数而不是整个命令,并在 []
.
内添加空格
gitall() {
git add .
if [ "" != "" ] # or better, if [ -n "" ]
then
git commit -m ""
else
git commit -m update
fi
git push
}
*:大多数 shell 不允许在别名中使用参数,我相信 csh 和派生词允许,但是 you shouldn't be using them anyway.
如果您出于某种原因确实需要使用带参数的别名,您可以通过在别名中嵌入一个函数并立即执行来破解它:
alias example='f() { echo Your arg was . };f'
我看到这种方法在 .gitconfig 别名中使用了很多。
我在 .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
}
这里git push origin HEAD
负责将你当前的分支推送到远程。
从命令提示符 运行 命令:gitall "commit message goes here"
如果我们只是 运行 gitall
而没有任何提交消息,那么提交消息将是 update
如函数所述。
我尝试了已接受的答案(凯文的),但出现以下错误
defining function based on alias `gitall'
parse error near `()'
因此根据 git issue 将语法更改为此,它起作用了。
function gitall {
git add .
if [ "" != "" ]
then
git commit -m ""
else
git commit -m update
fi
git push
}
带参数的别名
长话短说:
使用带参数的别名:
alias foo='echo bar'
# works:
foo 1
# bar 1
foo 1 2
# bar 1 2
已展开
(Space-separated) 别名后的字符将按照您编写的顺序被视为参数。
不能像使用函数那样对它们进行排序或更改。
例如在函数或子 shell 的帮助下,确实可以通过别名将参数放入命令中间:
参见 @Tom's answer
此行为类似于 bash
。
我正在尝试为我的简单 git add/commit/push.
创建一个带有参数的别名我看到一个函数可以用作别名,所以我试了但没成功。
之前我有:
alias gitall="git add . ; git commit -m 'update' ; git push"
但我希望能够修改我的提交:
function gitall() {
"git add ."
if [ != ""]
"git commit -m "
else
"git commit -m 'update'"
fi
"git push"
}
"git add ."
和 "
之间的其他命令只是 bash 的字符串,删除 "
s。
您可能想在 if 正文中使用 [ -n "" ]
。
你不能创建带参数的别名*,它必须是一个函数。您的功能很接近,您只需要引用某些参数而不是整个命令,并在 []
.
gitall() {
git add .
if [ "" != "" ] # or better, if [ -n "" ]
then
git commit -m ""
else
git commit -m update
fi
git push
}
*:大多数 shell 不允许在别名中使用参数,我相信 csh 和派生词允许,但是 you shouldn't be using them anyway.
如果您出于某种原因确实需要使用带参数的别名,您可以通过在别名中嵌入一个函数并立即执行来破解它:
alias example='f() { echo Your arg was . };f'
我看到这种方法在 .gitconfig 别名中使用了很多。
我在 .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
}
这里git push origin HEAD
负责将你当前的分支推送到远程。
从命令提示符 运行 命令:gitall "commit message goes here"
如果我们只是 运行 gitall
而没有任何提交消息,那么提交消息将是 update
如函数所述。
我尝试了已接受的答案(凯文的),但出现以下错误
defining function based on alias `gitall'
parse error near `()'
因此根据 git issue 将语法更改为此,它起作用了。
function gitall {
git add .
if [ "" != "" ]
then
git commit -m ""
else
git commit -m update
fi
git push
}
带参数的别名
长话短说:
使用带参数的别名:
alias foo='echo bar'
# works:
foo 1
# bar 1
foo 1 2
# bar 1 2
已展开
(Space-separated) 别名后的字符将按照您编写的顺序被视为参数。
不能像使用函数那样对它们进行排序或更改。 例如在函数或子 shell 的帮助下,确实可以通过别名将参数放入命令中间: 参见 @Tom's answer
此行为类似于 bash
。