126

I have the following command:

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

How do I make an alias out of it? I have tried:

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

4 Answers 4

158

You just need to escape it correctly.

alias xxx="svn status | awk '\$1 ==\"M\"{print \$2;}'"
7
  • 28
    So I wasn't escaping the $.. didn't know that! Nov 21, 2013 at 2:21
  • 8
    Nice! I didn't know I had to escape the '$' either!
    – Shadoninja
    Mar 29, 2016 at 20:36
  • 2
    Good answer but also a reminder how easy it is to make mistakes. In the case I was handling there were already escaped characters and it made a huge mess. EJK's answer below was more useful for me, a function is probably a cleaner option for many :)
    – jerclarke
    Nov 1, 2016 at 18:26
  • 1
    @jeremyclarke I don't disagree, escaping is hard, avoid it if you can.
    – ffledgling
    Nov 2, 2016 at 8:23
  • Does ` need to be escaped? Dec 20, 2018 at 6:46
128

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.

8
  • 14
    +1, but one quibble -- the function keyword is a needless incompatibility with POSIX. Just leave it off: xx() { ... Nov 21, 2013 at 2:16
  • 3
    This is simpler and I like it, but the other one answered the question more precisely.. Nov 21, 2013 at 2:23
  • 16
    @pragmatic_programmer That's a fair interpretation. Personally, I still hold that this answer is the better one despite not being responsive in a literal sense -- at least 50% of the time we get questions about aliases in irc.freenode.org's #bash, the answer is "use a function instead"; aliases can do nothing but prefix expansion, which isn't useful if you want to, for instance, branch on arguments. Nov 21, 2013 at 13:02
  • 13
    There are times when a literal answer to the exact question is much more preferred, but most of the time, an answer like this one is much more helpful because it 1) solves the initial problem in an easy to understand way and 2) gives the asker a new tool for their proverbial toolbelt.
    – TecBrat
    Mar 2, 2014 at 3:28
  • 1
    I have to agree, this is way better than trying to use alias
    – calder-ty
    Dec 6, 2017 at 4:03
13

Since Bash 2.04 there is a third (easier) way beside using a function or escaping the way @ffledgling did: using string literal syntax (here is an excellent answer).

So for example if you want 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.

This brings a shellcheck warning you could probably safely disable with # shellcheck disable=SC2139.

3

You can revert the double and simple quotations, so that double quotations are outside of single quotations.

For example, this does not work:

alias docker-table='docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"'

But this works:

alias docker-table="docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}'"

And when you check the actual alias interpreted, you can see the inner quotations are actually escaped.

$ alias docker-table
alias docker-table='docker ps --format '\''table {{.ID}}\t{{.Image}}\t{{.Status}}'\'''
1
  • The other solutions were not working for me.. But this worked.. Thanks..
    – ezvine
    Nov 1, 2022 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.