Hello all, I wan to create an alias of this command: alias dockps = "docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}""

The syntax for creating an alias is: alias $COMMAND = "docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}""

However, since there are quote marks, I assume they neet to be escaped with \. But in the case above, I’m getting the errors in fish and bash.

Fish error: $ alias dockps = "docker ps --format \"table {{.ID}} {{.Names}} {{.Status}} {{.Ports}}\""

alias: expected <= 2 arguments; got 3

Bash error: $ alias dockps = "docker ps --format \"table {{.ID}} {{.Names}} {{.Status}} {{.Ports}}\"" bash: alias: dockps: not found bash: alias: =: not found bash: alias: docker ps --format "table {{.ID}} {{.Names}} {{.Status}} {{.Ports}}": not found

What am I doing wrong?

Thanks in advance!

Edit: For fish shell users out there, this can be accomplished by using func: $ function dockerps docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}" end $ funcsave dockerps

I’m leaving the question up as the question with escape characters is still relevant and can be a learning resouce.

  • calm.like.a.bomb@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    19
    ·
    9 months ago

    As the others have said, your first issue is using blank spaces before and after =

    Then, when you need to use double quotes in a command, the alias should be defined with single quotes, like this:

    \$ alias dockps='docker ps --format "table {{.ID}}  {{.Names}}  {{.Status}}  {{.Ports}}"'
    
    • krash@lemmy.mlOP
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      9 months ago

      Thank you (and all others who replied), this worked flawlessly :-)

    • krash@lemmy.mlOP
      link
      fedilink
      arrow-up
      2
      ·
      8 months ago

      OMG! I didn’t even know about this, thanks! Will look into it, would be awesome to have ps command spit out things like I want them by default :-)

  • humanplayer2@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    9 months ago

    I think you have to alternate the quotations you use between doubles and singles, pairwise. Else the first pair is closed after --format.

    So you have to use a pattern like “command level 1 ‘level 2 “level 3” more level 2’ more level 1”

    Does that make sense?

    • friend_of_satan@lemmy.world
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      8 months ago

      That’s not how it works. The second bare double-quote closes the first one regardless of how it is nested in a string. The middle pair of double-quotes would need to be escaped. Also, single-quotes cannot be escaped in this way.

      The only place I can think of where nested double quotes do work is in subshells

      echo "hello $(echo "world")"
      

      This is because the subshell is interpreted before the outer logic, so during interpretation of the outer logic there is never a nested double quote, just the stdout of the subshell.

      These things are sometimes difficult to grok, and even more common, difficult to spot with human eyes. Best to use shellcheck, which will surely help you get better at shell scripting.

  • JDubbleu@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    9 months ago

    Not at my computer so I can’t double check, but I believe you can replace the outer double quotes with single quotes. I’d also remove the spaces before and after the equal sign for the alias. I don’t know about fish but I know bash doesn’t like when you add spaces there.