Linux 带有单引号和双引号的 bash 别名命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20111063/
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
bash alias command with both single and double quotes
提问by pragmatic_programmer
I have this command that does what I want but I can't get to alias it in my .bashrc (note that it uses both single and double quotes):
我有这个命令可以执行我想要的操作,但我无法在我的 .bashrc 中为其添加别名(请注意,它同时使用单引号和双引号):
svn status | awk ' =="M"{print ;}'
I've tried:
我试过了:
alias xx="svn status | awk ' ==\"M\"{print ;}'"
And some other common sense combinations with no luck.. I know that bash is very picky with quotes.. So what's the correct way to alias it and why ? Thanks
还有其他一些没有运气的常识组合..我知道bash对引号非常挑剔..那么正确的别名方法是什么,为什么?谢谢
采纳答案by ffledgling
You just need to escape it correctly.
你只需要正确地逃避它。
alias xxx="svn status | awk '$1 ==\"M\"{print $2;}'"
回答by EJK
Here's something that accomplishes the same thing without using an alias. Put it in a function in your .bashrc:
这是在不使用别名的情况下完成相同事情的东西。把它放在你的 .bashrc 中的一个函数中:
xx() {
svn status | awk ' =="M"{print ;}'
}
This way you don't have to worry about getting the quotes just right. This uses the exact same syntax you would at the command line.
这样您就不必担心报价是否正确。这与您在命令行中使用的语法完全相同。
回答by Pablo A
Since Bash 2.04there is a third (easier) way beside using a function or escaping the way @ffledgling did: using string literal syntax (here is an excellent answer).
从Bash 2.04 开始,除了使用函数或逃避@ffledgling 所做的方式之外,还有第三种(更简单的)方法:使用字符串文字语法(这是一个很好的答案)。
So for example if you want to make an alias of this onlinerit will end up being:
因此,例如,如果您想为这个在线用户创建一个别名,它最终将是:
alias snap-removedisabled=$'snap list --all | awk \'~"disabled"{print " --revision "}\' | xargs -rn3 snap remove'
So you just have to add the $
in front of the string and escape the single quotes.
所以你只需要$
在字符串前面添加并转义单引号。
This brings a shellcheck warningyou could probably safely disable with # shellcheck disable=SC2139
.