122

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;}'"
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
pragmatic_programmer
  • 3,566
  • 3
  • 29
  • 36

4 Answers4

153

You just need to escape it correctly.

alias xxx="svn status | awk '\$1 ==\"M\"{print \$2;}'"
ffledgling
  • 11,502
  • 8
  • 47
  • 69
125

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.

Marcus Buffett
  • 1,289
  • 1
  • 14
  • 32
EJK
  • 12,332
  • 3
  • 38
  • 55
  • 14
    +1, but one quibble -- the `function` keyword is a needless incompatibility with POSIX. Just leave it off: `xx() { ...` – Charles Duffy Nov 21 '13 at 02:16
  • 3
    This is simpler and I like it, but the other one answered the question more precisely.. – pragmatic_programmer Nov 21 '13 at 02: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. – Charles Duffy Nov 21 '13 at 13:02
  • This is indeed the better answer (and easier to handle) answer. – ffledgling Nov 21 '13 at 13:59
  • @CharlesDuffy I thoroughly agree this is the best answer, I've choosen the other just on fairness, but is not my interpretation, I asked how to quote an alias, he responded.. not an easy choice still! thanks – pragmatic_programmer Nov 21 '13 at 16:07
  • 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 02 '14 at 03:28
  • 1
    I have to agree, this is way better than trying to use alias – calder-ty Dec 06 '17 at 04: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.

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
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}}'\'''
WesternGun
  • 11,303
  • 6
  • 88
  • 157