bash alias command with both single and double quotes

49,816

Solution 1

You just need to escape it correctly.

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

Solution 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.

Solution 3

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.

Solution 4

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}}'\'''
Share:
49,816

Related videos on Youtube

pragmatic_programmer
Author by

pragmatic_programmer

Updated on November 25, 2021

Comments

  • pragmatic_programmer
    pragmatic_programmer over 2 years

    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

  • Charles Duffy
    Charles Duffy over 10 years
    +1, but one quibble -- the function keyword is a needless incompatibility with POSIX. Just leave it off: xx() { ...
  • pragmatic_programmer
    pragmatic_programmer over 10 years
    So I wasn't escaping the $.. didn't know that!
  • pragmatic_programmer
    pragmatic_programmer over 10 years
    This is simpler and I like it, but the other one answered the question more precisely..
  • Charles Duffy
    Charles Duffy over 10 years
    @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.
  • ffledgling
    ffledgling over 10 years
    This is indeed the better answer (and easier to handle) answer.
  • pragmatic_programmer
    pragmatic_programmer over 10 years
    @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
  • TecBrat
    TecBrat about 10 years
    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.
  • Shadoninja
    Shadoninja about 8 years
    Nice! I didn't know I had to escape the '$' either!
  • jerclarke
    jerclarke over 7 years
    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 :)
  • ffledgling
    ffledgling over 7 years
    @jeremyclarke I don't disagree, escaping is hard, avoid it if you can.
  • calder-ty
    calder-ty over 6 years
    I have to agree, this is way better than trying to use alias
  • Aaron Franke
    Aaron Franke over 5 years
    Does ` need to be escaped?
  • ffledgling
    ffledgling over 5 years
    @AaronFranke Depends heavily on the context. Maybe you should create a new question? :)