`sudo` with command substitution

时间:2019-04-08 12:47:50

标签: linux bash docker

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?

2 个答案:

答案 0 :(得分:6)

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

sudo docker stop $(sudo docker ps -q)

答案 1 :(得分:0)

xargs also should work:

$ sudo docker ps -q | xargs sudo docker stop