Bedrock Wiki
  • Discord
  • Contribute
  • bedrock.dev
  • MS Learn
Beginner's Guide
  • Guide
    • 1. Introduction
      guide
    • 2. Add-Ons Explained
    • 3. Software & Preparation
    • 4. Project Setup
    • 5. Create a Custom Item
    • 6. Create a Custom Entity
    • 7. Blockbench: Modeling, Texturing & Animating
    • 8. Adding a Loot Table, Spawn Rule & Crafting Recipe
  • Extra
    • a. Understanding JSON
    • b. Download Example Packs
    • c. Troubleshooting
      help
    • d. Advanced Manifest
    • e. Format Versions
    • f. Project Setup Android
Animation Controllers
  • Intro to Animation Controllers
    guide
  • Entity Commands
  • AFK Detector
  • Death Commands
  • Molang into Scoreboard
  • Respawn Commands
Blocks
  • General
    • Intro to Blocks
      guide
    • Block Components
    • Block Tags
    • Block States
    • Block Traits
    • Block Permutations
    • Block Events
      Scripts
    • Block Event Migration
      help
    • Blocks as Items
    • Troubleshooting Blocks
      help
  • Visuals
    • Block Culling
    • Block Models
      guide
    • Block Texture Animation
    • Block Texture Variation
    • Block Tinting
  • Tutorials
    • Applying Constant Effects
      Scripts
    • Avoiding State Limit
    • Fake Blocks
      Scripts
    • Ore Loot Tables
      Scripts
    • Precise Interaction
      Scripts
    • Precise Rotation
      Scripts
    • Rotatable Blocks
  • Vanilla Re-Creations
    • Custom Crops
      Scripts
    • Custom Glass
    • Custom Glazed Terracotta
    • Custom Trapdoors
      Scripts
  • Documentation
    • Block Format History
    • Block Shapes
    • Block Sounds
    • Vanilla Block Models
Commands
  • General
    • Intro to Command Blocks
    • Functions
    • Block States
    • Coordinate System
    • NBT Commands
    • Scoreboard Operations
    • Understanding Selectors
  • Commands
    • Damage
    • Execute
    • Playanimation
    • Playsound
  • On Event Systems
    • On Player First Join
    • On Player Join
    • On Player Leave
    • On Player Death
    • On Player Respawn
    • On First World Load
  • Scoreboard Systems
    • Entity Counter
    • Scoreboard Timers
    • Comparing And Retrieving Scores
  • Techniques
    • Execute Logic Gates
    • MBE - Max's Block Entity
    • FMBE - A New Way to Create Display Entities
    • Look Detection
    • Movement Detections
    • Orbital Camera
  • Useful Creations
    • Custom Crafter
    • Multiplayer Position Rearrangement
      function
Concepts
  • contents.json
  • Emojis & Symbols
  • Molang
  • Namespaces
  • Overwriting Assets
  • Raw Text
  • Shaders
  • Sounds
  • Subpacks
  • Text and Localization
  • Texture Atlases
  • textures_list.json
Documentation
  • Shared Constructs
  • Advanced Molang
  • File Types
  • Fog IDs
  • Material Configuration Description
  • Menu Categories
  • Molang Queries
  • Pack Folder Structure
  • Sound Definitions
  • Vanilla Materials
Entities
  • General
    • Intro to Entities BP
      guide
    • Intro to Entities RP
      guide
    • Troubleshooting Entities
      help
    • Entity Events
    • Entity Properties
    • NPC Dialogues
    • Render Controllers
    • Spawn Rules
  • Tutorials
    • Convert Points Between Any Space (World, Entity, Bones)
    • Creating Boats
    • Detecting Other Entities
    • Disabling Team Damage
    • Dummy Entities
    • Entity Attacks
    • Entity Holds Item
    • Entity Movement
    • Entity Timers
    • Flying Entities
    • Introduction to AOE Clouds
    • Invulnerable Entities
    • Look at Entity
    • Sleeping Entities
    • Solid Entities
    • Spawning Tamed Entities
      Scripts
    • Village Mechanic
  • Documentation
    • Dummy Components
    • Non-Mob Runtime Identifiers
    • Projectiles
    • Runtime Identifiers
    • Vanilla Usage Components
    • Vanilla Usage Spawn Rules
Items
  • General
    • Intro to Items
      guide
    • Item Components
    • Item Tags
    • Item Events
      Scripts
    • Item Event Migration
      help
    • Troubleshooting Items
      help
  • Tutorials
    • Custom Armor
    • Custom Food
      Scripts
    • Custom Pottery Sherds
    • Custom Weapons
    • Equipment-Based Commands
    • High Resolution Items
    • Spawning Items
    • Throwable Items
  • Documentation
    • Enchantments
    • Attachables
    • Item Format History
    • Numerical Item IDs
    • Vanilla Item Identifiers
    • Vanilla Usage Components
JSON UI
  • General
    • Intro to JSON UI
      guide
    • Best Practices
      guide
  • Tutorials
    • Adding HUD Elements
    • Aseprite Animations
    • Buttons and Toggles
    • Modifying Server Forms
    • Preserve Title Texts
    • String to Number
  • Documentation
    • JSON UI Documentation
Loot, Recipes & Trading
  • General
    • Trading Behavior
  • Documentation
    • Loot Tables
    • Trade Tables
    • Recipes
    • Item Functions
  • Tutorials
    • Randomized Structure Loot
Meta
  • Add-On Performance
  • Style Guide
  • Useful Links
  • Using Schemas
  • Version Control
  • Q&A
    • Blocks and Items Q&A 2024/08/30
    • Deferred Technical Preview Q&A 2024/02/23
    • GameTest Q&A 2021/08/06
    • Scripting and Editor Q&A 2023/09/22
    • World Generation Q&A 2024/11/15
NBT
  • General
    • .mcstructure
  • Tutorials
    • Experiments in Minecraft Education
    • Extending Structure Limits
  • NBT in Depth
    • About NBT (Named Binary Tag)
    • NBT Libraries
    • Reading NBT Example
Particles
  • General
    • Intro to Particles
      guide
  • Tutorials
    • Disabling Particles
  • Documentation
    • Vanilla Particles
Scripting
  • General
    • Intro to Scripting
    • What is Script API?
    • API Modules
  • Tutorials
    • Block Placement Prevention
    • GameTests
    • Script Core Features
    • Script Forms
    • Script Requests API
    • Simple Chat Commands
  • Documentation
    • Engine Environment
    • Script Resources
    • Script Watchdog
    • Troubleshooting JavaScript
    • TypeScript
Servers
  • Software
    • Bedrock Server Software
  • Protocols
    • Bedrock Protocol
    • NetherNet Protocol
    • RakNet Protocol
Visuals
  • General
    • Introduction to Entity Visuals
      guide
    • Bedrock Modeling
    • Custom Death Animations
    • Effects in Animations
    • Material Creations
    • Materials
    • Math-Based Animations
    • Skin Packs
  • Tutorials
    • Entity Texture Animation
    • Glowing Entity Texture
    • Hurt Animations
    • Leash Position
    • Player Geometry
    • Remove Entity Shadows
    • Retexturing Spawn Eggs
  • Ideas
    • Structure Presentation
World Generation
  • General
    • Intro to World Generation
      guide
    • Biomes
      guide
    • Feature Types
    • Jigsaw Structures
  • Tutorials
    • Block Conditions for Features
    • Generating Custom Ores
    • Generating Custom Structures
    • Generating Patches
    • Heightmap Noise
  • Documentation
    • Biome Tags

Execute

easy
Execute
  • Introduction
  • Understanding Execution Context
  • Execute, and Why it Changed
  • Syntax
    • /execute as
    • /execute at
    • /execute in
    • /execute positioned
    • /execute align
    • /execute anchored
    • /execute rotated
    • /execute facing
    • /execute (if|unless)
    • /execute run
  • Examples and Upgrading Old Commands

Introduction ​

With the release of 1.19.50, the /execute command was given a syntax overhaul. While the syntax is now more verbose and longer to write, it allows much finer control over the contextual components of commands and adds support for conditions to commands, superseding the use of commands like /testfor, /testforblock, and /testforblocks.

Before we dive into the syntax and how to write it, we need to understand how the old /execute command worked, and what changed and why. This will make explaining the concepts found in the syntax easier.

Understanding Execution Context ​

For both beginners to commands and those well versed in how old /execute behaved, it may be a good idea to review the concept of a command's execution context.

In short, these are the parameters that affects how a command runs. Who the command will run as, also known as its executor; where the command will run, and in which dimension; and the rotation applied to the command are all parameters that can be changed.

Every command has this context applied to it, and this context changes depending on how the command runs. Commands fired from command blocks do not have an executor, and the position is set to that command block; commands ran from chat define the executor as the player, and it runs at the player's position.

Execute, and Why it Changed ​

The /execute command executes a command on behalf of one or more entities. The old syntax used to be this:

/execute <target> <position> <command>
/execute <target> <position> detect <position> <block> <data value> <command>
1
2

You specified a target to execute as, then the command's context would change to run as that target, and at that target. Any position changes were then always relative to that target.

While this is useful in most cases, it also forces the fact that a command's target and its position are always tied together (unless you were to manually insert world coordinates in place of <position>). It is also not very malleable in regards to making conditional statements, as you have to execute as an entity every time.

Back in the Summer of 2017 during the Update Aquatic's development, the developers of Minecraft: Java Edition were getting feedback from the community on how they can improve the /execute command's syntax, and the basic concept that was conceived is this: /execute takes an unlimited number of subcommands that manipulate certain aspects of the command in the order you specify, then a "run" subcommand is placed at the end to fire a command.

This allows for much greater control for what /execute can do to a command, and allows splitting up the executor and the command's position.

Syntax ​

Now, let us review the /execute syntax. They are as follows:

/execute as ​

Changes the executor of the command, or what the target selector @s will select.

/execute as <origin: target> -> execute
1

This does not change the position, rotation, or dimension context of the command.

If multiple targets are specified then a command is ran once for each target, where @s selects each entity in turn.

/execute at ​

Changes where the command runs, setting the command's position, rotation, and dimension context to the entity.

/execute at <origin: target> -> execute
1

This does not change the executor of the command, so @s will remain as whoever was targeted last.

If multiple targets are specified then a command is ran once for each target, setting the position, rotation, and dimension context to each target.

/execute in ​

Sets the dimension in which the command should run.

/execute in <dimension: string> -> execute
1

Currently accepted values are overworld, nether, and the_end.

For example, to execute as a target if in The End, run:

/execute in the_end positioned 0 -100 0 as @a[rm=1] run say I'm in the End dimension
1

Note: Change in dimension will respect that dimension's scale; going from the Overworld to The Nether will apply a scale of x0.125 to the position, and vice versa will apply a x8 scale to the position.

/execute positioned ​

Directly sets the position context of the command.

/execute positioned <position: x y z> -> execute
1

Sets the position of the command to specific values. Relative and local coordinates are based around the current position of the command.

/execute positioned as <origin: target> -> execute
1

Sets the position of the command to a target's location. This is similar to how /execute at works, but it only sets the command's position and not its rotation or dimension.

If multiple targets are specified then a command is ran once for each target, setting the position context to the target's position.

/execute align ​

Aligns the current position of the command to the block grid.

/execute align <axes: swizzle> -> execute
1

Aligning a position will floor it. This subcommand accepts any non-repeating combination of the letters "x", "y", and "z", and will floor the position along each axis specified.

To align a target to the center of a block, run:

/execute as <target> at @s align xyz run tp @s ~0.5 ~0.5 ~0.5
1

/execute anchored ​

Sets the anchor of the command to the executor's feet or eyes. Changing the anchor will affect the position where local coordinates will start at.

/execute anchored (eyes|feet) -> execute
1

The default anchor when executing at a target is their feet.

When the anchor is set to eyes, the command's local position is offset by some amount corresponding to the "eye height" of the current executor.

This offset should only apply to local coordinates, but it currently affects relative coordinates due to a bug: MCPE-162681.

Also, the anchored subcommand can only be instructed once within the same /execute command. A second anchored instruction will have no effect. This is due to another bug: MCPE-165051

/execute rotated ​

Directly sets the rotation context of the command.

/execute rotated <yaw: value> <pitch: value> -> execute
1

Sets the rotation of the command to specific values. Relative and local coordinates are based around the current rotation of the command. This defaults to 0 for both pitch and yaw, unless the rotation was changed prior.

/execute rotated as <origin: target> -> execute
1

Sets the rotation of the command to a target's rotation.

If multiple targets are specified then a command is ran once for each target, setting the rotation context to the target's rotation.

/execute facing ​

Sets the rotation of the command to face some position. This rotation is calculated based on the current position of the command.

/execute facing <position: x y z> -> execute
1

Sets the rotation to face a block position. Relative and local coordinates are based around the current position of the command.

/execute facing entity <origin: target> (eyes|feet) -> execute
1

Sets the rotation to face a target's position. Setting the anchor to feet will aim the rotation to face where they are currently standing, while setting the anchor to eyes will aim the command up at the "eye position" of that target (see /execute anchored).

If multiple targets are specified then a command is ran once for each target, setting the rotation context to face that target.

/execute (if|unless) ​

Prevents running a command based on a condition. If the condition is true then the command will continue, or stop otherwise.

/execute unless acts as the opposite, testing if the condition is false in order to continue.

/execute if entity <target: target> -> execute
1

Acts like /testfor. Returns true if the targets exist.

/execute if block <position: x y z> <block: string> -> execute
1

Acts like /testforblock. Returns true if the block at the specified location exists.

A data value or block state may additionally be specified, otherwise it ignores block states (acts as if it were set to -1).

/execute if blocks <begin: x y z> <end: x y z> <destination: x y z> (all|masked) -> execute
1

Acts like /testforblocks. It constructs a volume between the beginning and end positions, and returns true if the volume at the destination matches the original volume.

The parameter all tests that all blocks must match, while masked will ignore air blocks.

/execute if score <target: target> <objective: string> matches <range: integer range> -> execute
1

Tests if a specified score is a certain value. This uses the integer range syntax.

/execute if score <target: target> <objective: string> (=|<|<=|>|>=) <source: target> <objective: string> -> execute
1

Tests if a specified score matches some logical comparison to another score. Operators are equals (=), greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=).

/execute run ​

/execute run <command: command>
1

Runs a command using all of the currently applied context modifications. This subcommand always goes last in one /execute command.

This subcommand is not always required however; an /execute command ending with an if or unless subcommand is valid too, and will return the success of the test it performed.

Examples and Upgrading Old Commands ​

Since subcommands can be chained limitlessly, there really is a nearly infinite combination of arguments for an /execute command and they cannot all be listed. Instead, listed here are some common examples of commands.

The old functionality of /execute can be replicated with as <target> at @s. If you need a positional offset relative to the entity, add positioned. If you want to detect if a block is present, add if block. Here are some equivalents:

  1. Teleport with an offset.
yaml
# Old syntax:
/execute @p ~ ~1.62 ~ teleport @s ^ ^ ^3
# New syntax:
/execute as @p at @s positioned ~ ~1.62 ~ run teleport @s ^ ^ ^3
1
2
3
4
  1. Chaining multiple executes.
yaml
# Old syntax:
/execute @e[type=sheep] ~ ~ ~ execute @e[type=item,r=5] ~ ~ ~ detect ~ ~-1 ~ stone kill @s
# New syntax:
/execute at @e[type=sheep] as @e[type=item,r=5] at @s if block ~ ~-1 ~ stone run kill @s
1
2
3
4

(Note that we do not use as @e[type=sheep] at @s because we do not need to execute as the sheep; only the position is required in this context.)

Now for some examples of things that were not possible to do in one command, or were more difficult to perform before the new syntax was introduced.

yaml
# Testing a fake-player-name's score:
/execute if score game_settings var matches 3.. run say [Game] Difficulty set to Hard.

# Comparing if two scores are equal:
/execute as @a if score @s wiki:cash = @s wiki:bank run say My cash balance is equal to my bank balance.

# Test for an entity without targeting it:
/execute as @a at @s if entity @e[type=armor_stand,r=10] run gamemode survival @s
1
2
3
4
5
6
7
8

(Recommended) Read Next: Execute Logic Gates

Contributors

Edit Execute on GitHub

Bedrock Wiki by Bedrock OSS

"Minecraft" is a trademark of Mojang AB.

Bedrock OSS, Bedrock Wiki and bedrock.dev are not affiliated in any way with Microsoft or Mojang AB.

  • Privacy Policy
  • Join our Discord
  • Learn how to Contribute
  • Visit our Repository