bash 别名中的多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756756/
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
Multiple commands in an alias for bash
提问by yuriel
I'd like to define an alias that runs the following two commands consecutively.
我想定义一个连续运行以下两个命令的别名。
gnome-screensaver
gnome-screensaver-command --lock
Right now I've added
现在我已经添加了
alias lock='gnome-screensaver-command --lock'
to my .bashrc but since I lock my workstation so often it would be easier to just type one command.
到我的 .bashrc 但因为我经常锁定我的工作站,所以只输入一个命令会更容易。
回答by mouviciel
Try:
尝试:
alias lock='gnome-screensaver; gnome-screensaver-command --lock'
or
或者
lock() {
gnome-screensaver
gnome-screensaver-command --lock
}
in your .bashrc
在你的 .bashrc 中
The second solution allows you to use arguments.
第二种解决方案允许您使用参数。
回答by gpojd
The other answers answer the question adequately, but your example looks like the second command depends on the first one being exiting successfully. You may want to try a short-circuit evaluationin your alias:
其他答案充分回答了问题,但您的示例看起来像第二个命令取决于第一个成功退出。您可能想在别名中尝试短路评估:
alias lock='gnome-screensaver && gnome-screensaver-command --lock'
Now the second command will not even be attempted unless the first one is successful. A better description of short-circuit evaluation is described in this SO question.
现在,除非第一个命令成功,否则甚至不会尝试第二个命令。这个SO question 中描述了对短路评估的更好描述。
回答by lhunath
Aliases are meant for aliasingcommand names. Anything beyond that should be done with functions.
别名用于别名命令名称。除此之外的任何事情都应该用函数来完成。
alias ll='ls -l' # The ll command is an alias for ls -l
Aliases are names that are still associated with the original name. ll
is just a slightly specific kind of ls
.
别名是仍然与原始名称相关联的名称。 ll
只是一种稍微特殊的ls
.
d() {
if exists colordiff; then
colordiff -ur "$@"
elif exists diff; then
diff -ur "$@"
elif exists comm; then
comm -3 "" ""
fi | less
}
A function is a new command that has internal logic. It isn't simply a rename of another command. It does internal operations.
函数是具有内部逻辑的新命令。它不仅仅是对另一个命令的重命名。它执行内部操作。
Technically, aliases in the Bash shell language are so limited in capabilities that they are extremely ill suited for anything that involves more than a single command. Use them for making a small mutation of a single command, nothing more.
从技术上讲,Bash shell 语言中的别名的功能非常有限,以至于它们非常不适合涉及多个命令的任何事情。使用它们对单个命令进行小的修改,仅此而已。
Since the intention is to create a new command that performs an operation which internally will resolve in other commands, the only correct answer is to use a function here:
由于目的是创建一个新命令来执行一个内部将在其他命令中解析的操作,唯一正确的答案是在这里使用一个函数:
lock() {
gnome-screensaver
gnome-screensaver-command --lock
}
Usage of aliases in a scenario like this runs into a lot of issues. Contrary to functions, which are executed as commands, aliases are expanded into the current command, which will lead to very unexpected issues when combining this alias "command" with other commands. They also don't work in scripts.
在这样的场景中使用别名会遇到很多问题。与作为命令执行的函数相反,别名被扩展到当前命令中,当将此别名“命令”与其他命令组合时,这将导致非常意外的问题。它们也不适用于脚本。
回答by Sean Bright
Does this not work?
这不起作用吗?
alias whatever='gnome-screensaver ; gnome-screensaver-command --lock'
回答by Adnan
This would run the 2 commands one after another:
这将一个接一个地运行 2 个命令:
alias lock='gnome-screensaver ; gnome-screensaver-command --lock'
回答by Jonathan Leffler
So use a semi-colon:
所以使用分号:
alias lock='gnome-screensaver; gnome-screen-saver-command --lock'
This doesn't work well if you want to supply arguments to the first command. Alternatively, create a trivial script in your $HOME/bin directory.
如果您想为第一个命令提供参数,这将不起作用。或者,在您的 $HOME/bin 目录中创建一个简单的脚本。
回答by Neenus
Adding my 2 cents to the 11 year old discussion try this:
将我的 2 美分添加到 11 年的讨论中试试这个:
alias lock="gnome-screensaver \gnome-screensaver-command --lock"
alias lock="gnome-screensaver \gnome-screensaver-command --lock"
回答by Philip Rego
Add this function to your ~/.bashrc
and restart your terminal or run source ~/.bashrc
将此功能添加到您的~/.bashrc
并重新启动终端或运行source ~/.bashrc
function lock() {
gnome-screensaver
gnome-screensaver-command --lock
}
This way these two commands will run whenever you enter lock
in your terminal.
这样,只要您lock
在终端中输入,这两个命令就会运行。
In your specific case creating an alias
may work, but I don't recommend it. Intuitively we would think the value of an alias would run the same as if you entered the value in the terminal. However that's not the case:
在您的特定情况下,创建一个alias
可能有效,但我不建议这样做。直觉上,我们会认为别名的值会像您在终端中输入值一样运行。然而事实并非如此:
The rules concerning the definition and use of aliases are somewhat confusing.
有关别名的定义和使用的规则有些令人困惑。
and
和
For almost every purpose, shell functions are preferred over aliases.
对于几乎所有用途,shell 函数都比别名更受欢迎。
So don't use an alias unless you have to. https://ss64.com/bash/alias.html
所以除非必须,否则不要使用别名。 https://ss64.com/bash/alias.html
回答by ToleLee
I came into a problem when declaring aliases into ~/.bashrc
. My terminal did not recognize the aliases I declared in ~/.bashrc
. I learned from the article (attached on the bottom) that Mac OS X run login-shell
by default hence it calls ~/.bash_profile
instead of ~/.bashrc
.
在将别名声明为~/.bashrc
. 我的终端无法识别我在~/.bashrc
. 我从文章(附在底部)中了解到 Mac OS Xlogin-shell
默认运行,因此它调用~/.bash_profile
而不是~/.bashrc
.
Should you come into the same problem in declaring your aliases, you can refer to the following link to solve the problem:
如果您在声明别名时遇到同样的问题,您可以参考以下链接解决问题:
http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html