yuklia
yuklia

Reputation: 7863

Mac OS xargs command unpredictable behaviour

I am really confused with xargs behaviour running on Mac OS

I have image like:

docker images | grep "docregistry/"
docregistry/jackrabbit   latest

I want to remove it by a filter like:

docker images | grep "docregistry/" | awk '{print $1":"$2}' | xargs -0 docker rmi -f

But I got an error:

Error: No such image: docregistry/jackrabbit:latest

If I run docker rmi docregistry/jackrabbit everything works as expected.

Thanks in advance!

Upvotes: 0

Views: 316

Answers (1)

chepner
chepner

Reputation: 532053

-0 tells xargs that it should split its input into arguments for the docker command on null characters, but your pipeline doesn't prodcue any null characters. That means the newline that awk outputs is treated as part of the image name; the error is really telling you that the image docregistry/jackrabbit:latest\n does not exist. (The error would probably be more obvious if grep output more than one image name.)

Assuming that Docker images cannot (or, at least in this case, will not) contain whitespace, just drop the -0 from the xargs command.

Upvotes: 2

Related Questions