Updated:
The basic shape
/execute as <who> at <where> run <command>
The command you actually want goes after run. To strike lightning where the nearest player stands:
/execute at @p run summon minecraft:lightning_bolt ~ ~ ~
as and at
aschanges who runs the command (what@smeans).atchanges where it runs (what~ ~ ~means), including the facing direction.
/execute as @e[type=minecraft:zombie] run say test makes each zombie the sender, but the position stays where the command started. Add at @s to move the position to each zombie too. The as vs at guide shows more cases.
/execute as @e[type=minecraft:zombie] at @s run particle minecraft:flame ~ ~1 ~ 0.1 0.1 0.1 0 5
When a selector matches several entities, the command runs once for each. Test with say first to see how many times it fires.
Run only when something is true
/execute if entity @e[type=minecraft:zombie,distance=..10] run say A zombie is nearby
/execute as @a at @s if block ~ ~-1 ~ minecraft:gold_block run effect give @s minecraft:speed 1 1 true
/execute as @a if score @s coins matches 100.. run say I have 100 coins or more
The first line checks for a zombie within 10 blocks. The second gives Speed only to players standing on a gold block. The third picks players whose coins score is 100 or more: 100.. means "100 or more" and ..9 means "9 or less". Use unless for the opposite check.
Subcommands you will use most
| Subcommand | What it does | Example |
|---|---|---|
as | Changes the sender | as @a |
at | Moves position and rotation to the target | at @s |
positioned | Moves only the position | positioned ~ ~5 ~ |
if / unless | Continues only when the check passes or fails | if block ~ ~-1 ~ minecraft:gold_block |
in | Changes the dimension | in minecraft:the_nether |
store | Saves the result to a score or data (Java) | store result score @s count |
Subcommands run from left to right, and run comes last. You can build the chain with the execute generator.
Bedrock Edition
Since Bedrock 1.19.50, /execute uses the same as, at, if and run shape as Java. Distance is written r=10 instead of distance=..10. The old form /execute @e ~ ~ ~ … may not work in current versions.
When nothing happens
- No effect: check that
runis there and the condition is really true. Run only the condition part, such as/execute if entity @e[type=minecraft:zombie], and the game tells you whether it passed. - Happens in the wrong place: with
asbut noat, the position stays at the start point (the block itself, for a command block). - Runs too many times:
@eand@arepeat for every match. Narrow them withlimit=1or other filters. - Error messages: see execute command not working.
Check your command
The Command Error Doctor checks common syntax mistakes in your browser.