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)

/execute is a versatile that executes other commands in a variety of scenarios:

Syntax
  • Java Edition
Modifier sub-commands can change how the command is run:
  • /execute as <entity> <chained command> executes a command using the entity <entity> (but doesn't change position).
  • /execute at <entity> <chained command> executes a command using the position, rotation, and dimension of the <entity> (but doesn't change entity).
  • /execute positioned <pos> <chained command> executes a command using the position of <pos>.
  • /execute positioned as <entity> <chained command> executes a command using the position (x y z) of the entity but nothing else.
  • /execute align <axes> <chained command> executes a command after aligning the current position to the block grid (rounding down), <axes> is any combination of x y and z (for example: x,xz,zyx and yz).
    • Examples:
      • x=-1.8,y=2.3,z=5.9 using x will become x=-2,y=2.3,z=5.9
      • x=2.4,y=-1.1,z=3.8 using yxz will become x=2,y=-2,z=3
  • /execute facing <x y z> <chained command> executes a command as though the executor is facing x y z.
  • /execute facing entity <entity> (eyes|feet) <chained command> executes a command as though the executor is facing the entity's eyes or feet.
  • /execute rotated as <entity> <chained command> executes a command as though the executor is rotated in the direction of the entity.
  • /execute rotated <y x> <chained command> will run the command as though the executor is rotated in the specified direction.
  • /execute in <dimension> <chained command> executes a command as though the executor is in the specified dimension.
  • /execute anchored (feet|eyes) <chained command> will make the rest of this command use feet or eyes for ^ ^ ^ coordinates or facing commands.
Conditional sub-commands can let you prevent the command from running at all:
  • /execute (if|unless) block <pos> <block> <chained command> executes a command if (or unless) <pos> matches <block>.
  • /execute (if|unless) blocks <begin> <end> <destination> (all|masked) <chained command> executes a command if (or unless) the region between <begin> and <end> matches <destination>.
  • /execute (if|unless) entity <entity> <chained command> executes a command if (or unless) <entity> exists (returns 1 or more entities).
  • /execute (if|unless) score <target> <targetObjective> (<|<=|=|>=|>) <source> <sourceObjective> <chained command>executes a command if (or unless) <target>'s score relates to <source>'s score based on the chosen criterium.
  • /execute (if|unless) score <target> <objective> matches <range> <chained command> executes a command if (or unless) <target>'s score is in the range (ie 1, 1..5).
As replacement for /stats, a new sub-command store lets you store the result or success of a command somewhere:
  • result is the result of a command, which replaces these old stats: AffectedBlocks, AffectedEntities, AffectedItems, QueryResult.
  • success is how many times the command was successful. This is usually 0 or 1, but if the command split up (for example as @a) then it may be more than 1. This replaces SuccessCount.
  • The value will be stored when the full command has finished executing.
  • If a command isn't successful (success is 0), result will always be set to 0.
  • It will be made clear what the expected result of each command is.
  • /execute store (result|success) score <name> <objective> <chained command>
    • The value is stored into the scoreboard under <name> and <objective>.
    • The objective must exist, but unlike with /stats you don't need to set an initial value for <name>.
  • /execute store (result|success) block <pos> <path> (byte|double|float|int|long|short) <scale> <chained command>
    • The value is stored in the nbt data at path of the block at pos as a byte, double, float, int, long, or short.
  • /execute store (result|success) entity <target> <path> (byte|double|float|int|long|short) <scale> <chained command>
    • The value is stored in the nbt data at path of one target entity as a byte, double, float, int, long, or short.
    • Like /data, /execute store can't modify player nbt. Nbt inside the tag key of items in the player's Inventory or EnderItemsis an exception and can be modified by /execute store.
  • /execute store (result|success) bossbar <id> (value|max) <chained command>
    • The value is stored in the value or max setting of the boss bar with ID id.
  • Data paths look like this: foo.bar[0]."A [crazy name]".baz.
    • foo.bar means foo's child called bar.
    • bar[0] means element 0 of bar.
    • "quoted strings" may be used if a name of a key needs to be escaped.
  • Examples:
    • /execute store success score @a foo run say hi
    • /execute as @e[type=pig] at @s store success entity @s Saddle byte 1 if entity @p[distance=..5]
You can chain all sub-commands together.
  • After every sub-command you need to write another sub-command.
  • When you're done with chaining sub-commands, run lets you write the actual command to be executed.
    • / is no longer allowed before the command.
  • /execute as somebody at somebody run say hi
  • Bedrock Edition
execute <origin: target> <position: x y z> <command: command>
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> <block: Block> <data: int> <command: command>

Color added to make reading easier.

Arguments
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.
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: command
Specifies the command to be run. Must be a valid command.
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: Block (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, stone)
data: int (detect mode only)
Must be a valid block data for that type of block or -1 to match any block data.
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)
Tracking Pixel