Minecraft – Would it be possible to make a command block that has commands set when placed

minecraft-java-edition

Some backstory. I wanted to make a command block that had a simple command when placed: Kill all users and set block to air to stop the trigger.
However, I had no idea how to make it so a command block had commands already input on place.
Is this possible? (Version 1.16.5)

Best Answer

You can do this from scratch with /setblock:

setblock 12 23 34 minecraft:command_block[]{Command:"…"}

Your command goes inside those quotation marks right there.

If you are looking to get this command block as an inventory item, use BlockEntityTag like so:

give @p minecraft:command_block{BlockEntityTag:{Command:"…"}}

Beware as you will need to escape quotation marks in your inner command, which can lead to messes like these!