Open main menu

Minecraft Wiki β

execute
Restrictions

Operator

First introduced

1.8 (14w07a)
PE Alpha 0.16.0 (PE Alpha 0.16.0 build 1)

Executes a command on behalf of one or more other entities, with originating permissions, optionally on condition that a single-block /testforblock-style check passes.

Syntax
  • Java Edition
execute <entity> <x> <y> <z> <command …>
An alternate syntax allows the command to be executed only if a specific block is detected:
execute <entity> <x> <y> <z> detect <x2> <y2> <z2> <block> <dataValue|state> <command …>
  • Bedrock Edition
execute <origin: target> <position: x y z> <command: string>
An alternate syntax allows the command to be executed only if a specific block is detected:
execute <origin: target> <position: x y z> detect <detectPos: x y z> <detectBlock: string> <detectData: int> <command: string>

Color added to make reading easier.

Arguments
entity (BE: origin: target)
Specifies the target to be the command's executor. Must be a player name or target selector. If more than one entity is selected, the command will be run once as each of them.
x y z (BE: position: x y z)
Specifies the position from which to run the command. x and z must be within the range -30,000,000 to 30,000,000 (inclusive). Coordinates specified with tilde notation are relative to the target, not to the position of the command's execution.
command (PE: command: string)
Specifies the command to be run. Must be a valid command.
x2 y2 z2 (BE: detectPos: x y z) (detect mode only)
Specifies the position of the block to check. x2 and z2 must be within the range -30,000,000 to 30,000,000 (inclusive). May use tilde notation to specify distances relative to (x,y,z).
block (BE: detectBlock: string) (detect mode only)
Specifies the block ID which the block at (x2,y2,z2) must match for the command to run. block must be a valid block id (for example, minecraft:stone)
dataValue (BE: detectData: int) (detect mode only)
Must be a valid block data for that type of block or -1 to match any block data.
state (detect mode only)[Java Edition only]
Must be a valid block state for that type of block or * to match any block state.
Result
Fails if arguments are not specified correctly, if entity fails to resolve to one or more valid entities (named players must be online), if the checked block is not of the correct block id, data or state, or if the specified command fails.
On success, executes the specified command as if executed by the specified target(s) with operator-level permission at the specified coordinate.
Examples
To summon lightning bolts at the positions of every zombie:
execute @e[type=zombie] ~ ~ ~ summon lightning_bolt
To summon lightning bolts at the positions of every zombie standing on any type of sand:
execute @e[type=zombie] ~ ~ ~ detect ~ ~-1 ~ minecraft:sand -1 summon lightning_bolt
To summon 10 creepers at the nearest player's position (only works as long as there are at least 10 entities in the loaded world):
execute @e[c=10] ~ ~ ~ execute @p ~ ~ ~ summon creeper
See also
/testforblock — tests for a certain block at a specified position (including specific data tags)