bash alias command with both single and double quotes

限于喜欢 提交于 2019-11-26 15:42:10

问题


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):

svn status | awk '$1 =="M"{print $2;}'

I've tried:

alias xx="svn status | awk '$1 ==\"M\"{print $2;}'"

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


回答1:


You just need to escape it correctly.

alias xxx="svn status | awk '\$1 ==\"M\"{print \$2;}'"



回答2:


Here's something that accomplishes the same thing without using an alias. Put it in a function in your .bashrc:

xx() {
    svn status | awk '$1 =="M"{print $2;}'
}

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.




回答3:


There is a third way (I found easier) beside using a function or escaping the way @ffledgling did: using string literal syntax (here is an excellent answer).

So for example if you wan't to make an alias of this onliner it will end up being:

alias snap-removedisabled=$'snap list --all | awk \'$5~"disabled"{print $1" --revision "$3}\' | xargs -rn3 snap remove'

So you just have to add the $ in front of the string and escape the single quotes.



来源:https://stackoverflow.com/questions/20111063/bash-alias-command-with-both-single-and-double-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!