From Minecraft Wiki
< Commands
Revision as of 04:38, 8 January 2020 by NovaBot (talk | contribs) (Add Commands navbox)
Jump to: navigation, search

Commands/infobox/execute is a versatile command that modifies variables or procedures in the execution of another command, such as giving the server a false impression of the entity that's executing the command, its current position, how it's rotated, etc.

All commands end up having a success status and some quantifiable value they can return to a target, as might be specified by /execute store <arguments>. All commands issued from command blocks return the success status to that issuing command block, which can trigger chained command blocks.

Java Edition


There are twelve instructions (or sub-commands) for the /execute command, and each has its own special syntax, so describing syntax takes a large branching tree.

Full syntax tree
/execute . . .
  • align <axes> -> execute
  • anchored (eyes|feet) -> execute
  • as <targets> -> execute
  • at <targets> -> execute
  • facing (<pos>|entity <targets> (eyes|feet)) -> execute
  • in <dimension> -> execute
  • positioned (<pos>|as <targets>) -> execute
  • rotated (<rot>|as <targets>) -> execute
  • store (result|success) . . .
    • … block <targetPos> <path> (byte|short|int|long|float|double) <scale> -> execute
    • … bossbar <id> (max|value) -> execute
    • … entity <target> <path> (byte|short|int|long|float|double) <scale> -> execute
    • … score <targets> <objective> -> execute
    • … storage <target> <path> (byte|short|int|long|float|double) <scale> -> execute
  • … (if|unless) . . .
    • … block <pos> <block> -> [execute]
    • … blocks <start> <end> <destination> (all|masked) -> [execute]
    • … data . . .
      • … block <sourcePos> <path> -> [execute]
      • … entity <source> <path> -> [execute]
      • … storage <source> <path> -> [execute]
    • … entity <entities> -> [execute]
    • … predicate <predicate> -> [execute]
    • … score <target> <targetObjective> . . .
      • … (<|<=|=|>|>=) <source> <sourceObjective> -> [execute]
      • … matches <range> -> [execute]
  • run <command>
… where -> execute represents the start of another execution instruction. A number of instructions can be strung end-to-end, where run is the only instruction that can terminate the command—an exception is that including the if or unless instructions allow this terminator to be left out.
There are 11 chainable instructions—9 instructions that modify command variables and 2 conditional instructions—and a final run instruction for carrying out some following command. A number of instructions can be chained together, but run finalizes the command. The simplest usage for this command is: /execute run <command>.
Alternatively, using the conditional instructions if or unless without run is still valid syntax, in which case the result of the test is returned: /execute if entity @e[name="Fred"]
The conditional instructions if and unless can prevent a command from running at all. All specified conditions must be met for the test to pass and the command to run.
The 11 chainable instructions are:
  1. align <axes> -> execute
    Align the current position to the block grid, rounding down (towards negative infinity).
    <axes>—Any non-repeating combination of the characters 'x', 'y', and 'z'. (For example, x, xz, zyx, or yz.)
    Example: Given the position (-1.8, +2.3, +5.9), the command "/execute align xz run summon zombie ~ ~ ~" summons a zombie at (-2.0, +2.3, +5.0).
    Example: Given (+2.4, -1.1, +3.8), using "/execute align yxz run spawnpoint @p ~ ~ ~" sets the player's spawnpoint to (2, -2, 3).
  2. anchored (feet|eyes) -> execute
    Stores the distance from the feet to the eyes of the entity that is executing the command in the anchor, which is part of the command context.
    The only places where the anchor is used is in the caret notation (^ΔT ^ΔN ^ΔB) and in the facing subcommand of the execute command, which behave as if the executing position was shifted upwards by the stored value.
  3. as <targets> -> execute
    Execute the command as the targeted entity/entities, but does not change position. If an entity doesn't have an eye anchor point, the command anchors to the feet of the previous executor (not the specified entity).
    [These details need fact checking! These statements seem conflicting.[citation needed]]
    Changes the function of the @s target selector variable
  4. at <target> -> execute
    Use the position, rotation, and dimension of the target, but does not change the current executing entity (@s).
    Changes the function of ~Δx ~Δy ~Δz and ^ΔT ^ΔN ^ΔB positioning
  5. facing (<pos>|entity <target> (eyes|feet)) -> execute
    Use an execution position that faces either the given position or the targeted entity's eyes or feet.
    Changes the function of ^ΔT ^ΔN ^ΔB positioning
  6. in <dimension> -> execute
    Execute as though the executor is in the given dimension.
    Changes command dimension, but does not affect X- Y- Z-positions
    <dimension>—The standard dimensions in the Minecraft namespace are "overworld", "the_nether", and "the_end".
  7. positioned (<pos>|as <targets>) -> execute
    Use either the given coordinates or the targeted entity's coordinates for the "current position."
    Changes the function of ^ΔT ^ΔN ^ΔB and ~Δx ~Δy ~Δz positioning. Does not change rotation, dimension, or target of @s.
  8. rotated (<rot>|as <targets>) -> execute
    Execute as though the executor is rotated either in the given direction or the same the targeted entity is facing.
    Changes command rotation
  9. store (result|success) (block|bossbar|entity|score|storage) <arguments> -> execute
    Store the result or success of a command somewhere. This instruction replaced the "/stats" command.
    result—Return the result of the command. (This replaces the old stats AffectedBlocks, AffectedEntities, AffectedItems, QueryResult.)
    success—Return how many times the command was successful. This is usually 0 or 1, but if the command split up (as perhaps with "/execute as @a -> execute") then success can be a number higher than 1. (This replaces the old stat SuccessCount.)
    If a command fails, "success" and "result" both return 0. The return value is stored when the full command has finished executing.
    • … block <pos> <path> (byte|short|int|long|float|double) <scale> -> execute
      Saves the return value in NBT data at the given block position.
      <path>—Name of the data tag to save return value in. See 10. (if|unless) data below for more details.
    • … bossbar <id> (value|max) -> execute
      Saves the return value in either the current value or the maximum value of the given bossbar
      <id>—ID of the bossbar to store return value in.
      value—Specifies that the bossbar's "value" is to be overwritten with the execution's return value
      max—Specifies that the bossbar's "max" is to be overwritten with the execution's return value
    • … entity <target> <path> (byte|short|int|long|float|double) <scale> -> execute
      Use the data tag path from one targeted entity to save the return value. Store as a byte, short, int, long, float, or double.
      Like the "/data" command, "/execute store <arguments>" cannot modify player NBT.
    • … score <name> <objective> -> execute
      Use the scoreboard under <name> and <objective> to save the return value.
      <objective> must exist, but unlike with "/stats" you don't need to set an initial value for <name>.
    • … storage <target> <path> (byte|short|int|long|float|double) <scale> -> execute
      Uses the <path> within storage <target> to store the return value in.
      If the storage does not yet exist, it will be created.
      <target>—Target storage container, as a namespaced ID.
      <path>—Name of the data tag to save return value in. See 10. (if|unless) data below for more details.
  10. (if|unless) (block|blocks|data|entity|predicate|score) <arguments> -> [execute]
    Test for some condition before choosing whether to carry out the command.
    • … block <pos> <block> -> [execute]
      Compares the block at the given position to the given block ID
    • … blocks <start> <end> <destination> (all|masked) -> [execute]
      Compare region between the <start> and <end> positions to the block located at the <destination> position.
    • … data (block <pos>|entity <target>|storage <source>) <path> -> [execute]
      Execute if or unless the targeted block, entity or storage has any data for the tag named by <path>.
      <path>—Names an NBT to check for. This is a string of parent tag names of the Java-like form highestTag.child.childsChild…parent.targetDataTag.
      See Commands/data#NBT path for more on paths
      Example: /execute as @e if data entity @s ArmorItems[0].id would test for if any entity has an "id" for the item in their boot armor slot. Note that this returns true for all mobs, armored or not.
    • … entity <targets> -> [execute]
      Execute if or unless one or more <targets> exist.
    • … predicate <predicate> -> [execute]
      Execute if or unless the <predicate> evaluates to a positive result.
      See Predicate for more on predicates
    • … score <target> <targetObjective> (<|<=|=|>=|>) <source> <sourceObjective> -> [execute]
      Execute if or unless the target entity's score accurately relates to source entity's score, given some conditional operator.
    • … score <target> <targetObjective> matches <range> -> [execute]
      Execute if or unless target entity's score is within given range (e.g. 1, 1..5).
  11. unless <arguments> -> [execute]
    See 10. (if|unless) above.
Beyond those 11 instructions, there's the run instruction.
  • run <command>
    Specifies the command to be executed, the variables of which may be modified by the subcommands used.
    Can be any Minecraft command
    Technical: Resets the command node to the root of the command dispatcher
    Modelling commands as chat text...
    The command dispatcher is what starts when the player begins a message with a forward-slash (/).
    A command node is the specific word/entry your cursor is editing, either a command or an argument.
    The root node comes before the first word in your command.
  • /execute at @a anchored eyes run particle smoke ^ ^ ^3
  • /execute as @e[type=pig] at @s store success entity @s Saddle byte 1 if entity @p[distance=..5]
  • /execute as @a at @s if block ~ ~ ~ water run say "My feet are soaked!"

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.

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.
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), done with operator-level permission at the specified coordinate.
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 ~ ~ ~


Java Edition
1.814w07aAdded /execute.
14w08aCommands run using /execute will now pass their success value back to the command block running them.
1.1116w32aAdded block state support to /execute.
1.1317w45aThe syntax of /execute has been split off.
17w45bReworked /execute store (result|success).
18w02aAdded new subcommands to /execute to allow for more control over commands.
18w05aAdded /execute store (result|success).
1.1418w43aAdded /execute (if|unless) data.
1.1519w38aAdded /execute if predicate <predicate>.
Added /execute store storage.
Pocket Edition Alpha
0.16.0build 1Added /execute.
Functionality almost is equivalent to Java Edition 1.10 usage, except the entity target is optional in /execute.