bash 如何将参数传递给别名

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

how to passing arguments to an alias

bashalias

提问by toughtalker

i add an alias in .bashrc

我在 .bashrc 中添加了一个别名

alias sr='sudo /etc/rc.d/[parameter?] restart'

alias sr='sudo /etc/rc.d/[parameter?] restart'

sr network -> sudo /etc/rc.d/network restart

sr 网络 -> sudo /etc/rc.d/network restart

sr sshd -> sudo /etc/rc.d/sshd restart

sr sshd -> sudo /etc/rc.d/sshd 重启

could it be achieved, thanks!

能不能实现,谢谢!

回答by Laurence Gonsalves

Use a shell function instead. eg:

请改用 shell 函数。例如:

function sr () {
  sudo /etc/rc.d/"" restart
}

回答by SiegeX

I'm surprised nobody mentioned to use a function

我很惊讶没有人提到使用函数

function sr() { 
  sudo /etc/rc.d/"$@" restart 
}

回答by chrisaycock

Use a function rather than an alias:

使用函数而不是别名:

function sr() { sudo /etc/rc.d/$@ restart; }

回答by Jim Garrison

Aliases in bash do not accept parameters. However, you could define a function instead:

bash 中的别名不接受参数。但是,您可以改为定义一个函数:

function sr() { sudo /etc/rc.d/ restart; }