Updated:

A shulker box of diamonds

VersionCommand
Java 1.20.5+/give @p minecraft:shulker_box[container=[{slot:0,item:{id:"minecraft:diamond",count:64}}]]
Java 1.13–1.20.4/give @p minecraft:shulker_box{BlockEntityTag:{Items:[{Slot:0b,id:"minecraft:diamond",Count:64b}]}}

From 1.20.5 the contents use the container component and the amount is lowercase count.

Several items (1.20.5+)

/give @p minecraft:shulker_box[container=[{slot:0,item:{id:"minecraft:diamond_sword",count:1}},{slot:1,item:{id:"minecraft:bread",count:32}},{slot:2,item:{id:"minecraft:torch",count:64}}]]

Separate entries with commas. If the same slot appears twice, only the last one is used. Items can carry their own components, such as enchantments, written the same way as in /give.

Slot numbers

RowSlots
Top0–8
Middle9–17
Bottom18–26

Place a filled chest

/setblock ~ ~ ~1 minecraft:chest{Items:[{Slot:0b,id:"minecraft:golden_apple",count:3}]}

A placed block uses block data (Items), not item components. Use count on 1.20.5+ and Count:3b before. Facing goes in square brackets before the data: minecraft:chest[facing=north]{…}. Barrels work the same way.

Add items to a chest that is already placed

/item replace block ~ ~ ~1 container.0 with minecraft:diamond 64

This puts 64 diamonds in the first slot of the chest one block south of you (Java 1.17+). It replaces whatever was in that slot, so other slots stay as they are. On Bedrock, use /replaceitem block ~ ~ ~1 slot.container 0 diamond 64.

Fill a chest with random loot (Java)

/setblock ~ ~ ~1 minecraft:chest{LootTable:"minecraft:chests/simple_dungeon"}

The chest is filled from the dungeon loot table the first time a player opens it, so every chest can roll different items. Other tables include chests/desert_pyramid and chests/end_city_treasure.

Colored boxes

Put the color first, as in minecraft:red_shulker_box. The 16 colors are white, orange, magenta, light_blue, yellow, lime, pink, gray, light_gray, cyan, purple, blue, brown, green, red and black.

Common problems

Check your command

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