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

/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

SubcommandWhat it doesExample
asChanges the senderas @a
atMoves position and rotation to the targetat @s
positionedMoves only the positionpositioned ~ ~5 ~
if / unlessContinues only when the check passes or failsif block ~ ~-1 ~ minecraft:gold_block
inChanges the dimensionin minecraft:the_nether
storeSaves 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

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.