bash 哦,我的 Zsh 具有一个别名的多个命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19255030/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:13:47  来源:igfitidea点击:

Oh My Zsh multiple commands with one alias

customizationzshbashoh-my-zsh

提问by snakesonatoni

I'm using Oh My Zsh, and was wondering if there is a way to create a function or alias to run multiple commands. Just as an example, running an 'update' command will update specific gems, but not all of them.

我正在使用Oh My Zsh,想知道是否有办法创建一个函数或别名来运行多个命令。举个例子,运行“更​​新”命令将更新特定的 gems,但不是全部。

回答by simont

As you've discovered, you can chain commands in a single alias using ;:

正如您所发现的,您可以使用;以下命令在单个别名中链接命令:

alias update_my_gems="echo foo; echo bar"

Alternatively, you can write a function very easily in your ~/.zshrcfile:

或者,您可以非常轻松地在~/.zshrc文件中编写一个函数:

update_my_gems() {
    echo foo
    echo bar
}

For readability, I'd personally go for a function for anything that's semi-complex.

为了可读性,我个人会为任何半复杂的东西寻找一个函数。

回答by cesartalves

If there are many commands, I find it useful to alias the execution of a .sh file located on my home directory

如果有很多命令,我发现将位于我的主目录中的 .sh 文件的执行别名化很有用

alias start_containers="./start-containers.sh"

To throw the alias inside the config file, you can do

要将别名放入配置文件中,您可以执行以下操作

echo alias start_containers="./start-containers.sh" >> ~/.zshrc