Minecraft – How to use testfor with a mob with a two-word name

minecraft-commandsminecraft-java-edition

How can I test for mob who has a name with two words? For example villager named Grumpy Bill.
For example I want to kill this villager.

This command

/kill @e[name=Bill]

would work just fine but when I try

/kill @e[name=Grumpy Bill]

it says "The entity UUID provided is in an invalid format"

What is the proper command for this action?

Best Answer

In 1.13 and above you can add quotes to the name in the selector, like so:

/kill @e[name="Grumpy Bill"]

In 1.12 and below a selector cannot test for a name with spaces or most symbols; it is marked on the bug tracker as "Won't Fix" (but is fixed in 1.13+).

You can however select them by NBT data with their "CustomName" tag. Like this:

/testfor @e {CustomName:"Grumpy Bill"}

If you want to do something other than test for them existing, you'll need to create a dummy scoreboard objective, objectiveName, and then run something like this:

/scoreboard players set @e objectiveName 1 {CustomName:"Grumpy Bill"}

And then:

/kill @e[score_objectiveName_min=1]