Skip to content
Advertisement

`sudo` with command substitution

I’m working with a remote machine that requires sudo for most docker commands. I often have to perform multiple repetitive commands to stop docker containers, etc: sudo docker stop <container_name>.

Normally, I’d use docker stop $(docker ps -q) to stop all the results of docker ps -q, but simply prefixing this with sudo doesn’t do what I’m after: sudo docker stop $(docker ps -q) results in a permission denied error.

Is there a way to pipe this through sudo so that I don’t have to stop everything individually?

Advertisement

Answer

You also need to specify sudo` in the inner command. So the following should work:

sudo docker stop $(sudo docker ps -q)
Advertisement