Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh alias to remove all docker containers returns command not found

Tags:

zsh

zshrc

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?

like image 769
alohamaloha Avatar asked Nov 02 '25 20:11

alohamaloha


1 Answers

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.

like image 90
Adaephon Avatar answered Nov 04 '25 18:11

Adaephon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!