3

I have this alias in my .zshrc file:

alias rmcons="docker rm -f $(docker ps -aq)"

But after trying to execute it, it removes only one container and then it prints

$rmcons
ef8197f147fb
zsh: command not found: c2ea2673f9e4
zsh: command not found: 4603059f1618
zsh: command not found: 40ad60328595

How can I remove all containers that docker ps -aq shows?

Adaephon
  • 16,929
  • 1
  • 54
  • 71
alohamaloha
  • 147
  • 1
  • 12

2 Answers2

14

You need to use single quotes ('') instead of double quotes ("").

alias rmcons='docker rm -f $(docker ps -aq)'

If you use double quotes, than the command substitution $(docker ps -aq) will be evaluated when you define the alias. In your example this was equivalent to

alias rmcons="docker rm -f ef8197f147fb
c2ea2673f9e4
4603059f1618
40ad60328595"

As the newlines are command separators (like ;) this alias is substituted by four commands: docker rm -f ef8197f147fb, c2ea2673f9e4, 4603059f1618 and 40ad60328595. The last three of which do not exist on your system, hence "command not found". It also means that the same output of docker ps -aq - as it was on alias definiton - will be used and not as it would be when running the alias.

On the other hand, if you use single quotes, the alias will actually substituted by the exact command you defined: docker rm -f $(docker ps -aq). Although docker ps -aq will still return output with newlines, these newlines are now only parsed word separators between arguments.

Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • 1
    Thanks! Just ran it, and it worked perfectly, didn't know about such difference – alohamaloha Jul 06 '17 at 21:06
  • Why does the shortcut `alias dodownall='docker kill $(docker ps -q)'` still returns `"docker kill" requires at least 1 argument.`? The same when I try your command above. – nonNumericalFloat Dec 10 '21 at 16:39
  • @nonNumericalFloat That should only happen if the output of `docker ps -q` is empty (or contains only whitespace characters). The most likely cause for that would be that no containers are running (which might for example be caused by a prior run of `dodownall`). Does running `docker ps -q` show anything for you in the cases where `dodownall` fails with the missing argument error? – Adaephon Dec 10 '21 at 16:54
  • Oh damn, nevermind. Silly me forgot to open a new bash... Thank you! I allowed myself to do a little edit ;D – nonNumericalFloat Dec 10 '21 at 17:00
-1

Warning: untested. I don't use/have docker.

I think you should serialize the output first "escaping" the new lines.


You might also use the for loop, trying:

for id in `docker ps -aq`; do docker rm -f $id; done

Note the backquotes to parse the command's output.

You can also directly use $() instead of its shortcut backquote.

I recommend to test with echo first instead of removing with rm:

for id in `docker -ps -aq`; do echo rm -f $id; done

, and to use the rm with its -i switch to prompt for confirmation before deleting.

I hope docker's rm subcommand has one.