What Is Bitcoin Script? A Beginner’s Guide to Bitcoin’s Programming Language
Meta Description: Learn what Bitcoin Script is, how locking and unlocking scripts work, common opcodes, script types like P2PKH and Taproot, and why Bitcoin Script is intentionally limited.
Every time you send bitcoin, a small program runs behind the scenes to decide whether you are allowed to spend those coins. That program is written in Bitcoin Script. You never see it in a normal wallet, but it is the rulebook that protects every coin on the network.
In this guide, you will learn what Bitcoin Script is, how it works step by step, which script types exist, and why its simplicity is a feature, not a flaw.
What Is Bitcoin Script?
Bitcoin Script is the simple, stack-based programming language used to define the conditions for spending bitcoin. Every Bitcoin output contains a small piece of code that says, in effect, “to spend these coins, you must satisfy this condition.”
What Makes a Bitcoin Block Valid? Understanding Bitcoin's Consensus Rules
In most cases the condition is: “provide a valid signature from the owner of this address.” But Script can express more complex rules too, such as requiring multiple signatures or locking coins until a future date.
If you have read our guide on Bitcoin inputs and outputs, Script is the mechanism that connects them: outputs lock coins with a script, and inputs unlock them by satisfying it.
Key Features of Bitcoin Script
- Stack-based: Data is pushed onto a stack and operations act on the top items, like a pile of plates where you only touch the top.
- Simple and compact: It uses short commands called opcodes instead of complex code.
- Not Turing complete: There are no loops, so a script cannot run forever. This is intentional.
- Deterministic: The same script always gives the same result on every node, with no outside data or randomness.
- Executed by every node: Every full node independently validates scripts before accepting a transaction.
Locking and Unlocking Scripts
Each spend involves two pieces:
- Locking script (scriptPubKey): Placed in the output. It sets the conditions for spending those coins.
- Unlocking script (scriptSig or witness): Placed in the input of the spending transaction. It supplies the data, usually a signature and public key, that satisfies the lock.
Think of a padlock and a key. The locking script is the padlock attached to the coins. The unlocking script is the key you present. Nodes check whether the key opens the lock. If it does, the spend is valid. If not, the transaction is rejected.
What Are Opcodes?
Opcodes are the individual commands in Bitcoin Script. Some common ones:
| Opcode | What it does |
|---|---|
| OP_DUP | Duplicates the top item on the stack |
| OP_HASH160 | Hashes the top item (SHA-256, then RIPEMD-160) |
| OP_EQUALVERIFY | Checks two items are equal; fails the script if not |
| OP_CHECKSIG | Verifies a signature against a public key |
| OP_CHECKMULTISIG | Verifies several signatures against several public keys |
| OP_RETURN | Marks an output as unspendable, often used to attach small amounts of data |
| OP_CHECKLOCKTIMEVERIFY | Prevents spending until a certain time or block height |
| OP_CHECKSEQUENCEVERIFY | Prevents spending until a set time has passed since the output was confirmed |
Bitcoin also disabled several opcodes early in its history (for example, multiplication) to reduce risk and keep validation simple.
How Bitcoin Script Works: A P2PKH Example
The classic script type is Pay-to-Public-Key-Hash (P2PKH), used by legacy addresses starting with “1”.
Locking script:
OP_DUP OP_HASH160 <public key hash> OP_EQUALVERIFY OP_CHECKSIG
Unlocking script:
<signature> <public key>
Here is how the stack processes them:
| Step | Action | Result |
|---|---|---|
| 1 | Push the signature and public key | Stack holds both |
| 2 | OP_DUP | Public key is duplicated |
| 3 | OP_HASH160 | The copy is hashed |
| 4 | Push the public key hash from the locking script | Hash from the lock is added |
| 5 | OP_EQUALVERIFY | Confirms the two hashes match, so the public key is the right one |
| 6 | OP_CHECKSIG | Confirms the signature is valid for that public key |
If the final result is “true,” the spend is valid. The signature proves ownership without ever revealing the private key.
Common Bitcoin Script Types
| Script Type | Address Prefix | Description |
|---|---|---|
| P2PK | (none) | Pays directly to a public key. Used in Bitcoin’s earliest days |
| P2PKH | 1... | Pays to a hash of a public key. The original standard |
| P2SH | 3... | Pays to a hash of a script, enabling multisig and more complex conditions |
| P2WPKH / P2WSH | bc1q... | SegWit versions that move signature data into the witness, improving efficiency |
| P2TR (Taproot) | bc1p... | Introduced in 2021. Improves privacy and flexibility using Schnorr signatures |
Newer script types generally offer lower fees and better features, which is why most modern wallets use SegWit or Taproot addresses by default.
What Can Bitcoin Script Do?
Despite being simple, Script supports several useful patterns:
- Multisignature (multisig): Require, for example, 2 of 3 keys to approve a spend. Common for shared funds and stronger security.
- Timelocks: Lock coins until a future time or block height. Useful for inheritance planning and payment channels.
- Hash locks: Require revealing a secret to spend. Used in some payment channel and swap designs.
- Payment channels: Technologies like the Lightning Network rely on script conditions to work safely.
- Data embedding: OP_RETURN lets people attach small pieces of data to the blockchain.
Bitcoin Script vs Ethereum Smart Contracts
Both can enforce rules on money, but they differ in design:
| Bitcoin Script | Ethereum Smart Contracts | |
|---|---|---|
| Language | Simple, stack-based | Full programming languages (e.g., Solidity) |
| Loops | None | Supported |
| Complexity | Limited by design | Highly flexible |
| Risk surface | Smaller | Larger |
Bitcoin prioritizes security and predictability over flexibility. Fewer features means fewer ways for things to go wrong.
Why Is Bitcoin Script Intentionally Limited?
- Security: A simple language is easier to audit and has fewer bugs.
- Predictability: Without loops, every script finishes in a bounded amount of time.
- Resource protection: Nodes cannot be forced to run endless or heavy computations.
- Decentralization: Lightweight validation keeps it practical for ordinary people to run a full node.
Frequently Asked Questions
Is Bitcoin Script the same as a smart contract?
Not exactly. Script can enforce simple spending conditions, so some call it a basic form of smart contract, but it is far more limited than platforms designed for general-purpose contracts.
Do I need to learn Bitcoin Script to use Bitcoin?
No. Wallets handle scripts automatically. Learning it is only needed if you want to understand how Bitcoin works deeply or build on it.
Why doesn’t Bitcoin Script have loops?
Loops could let a script run indefinitely, which would be a risk to the network. Removing them keeps validation safe and predictable.
Can Bitcoin Script be upgraded?
Yes, through carefully reviewed soft forks. SegWit (2017) and Taproot (2021) are examples of upgrades that expanded what Script can do.
What is OP_RETURN used for?
It creates a provably unspendable output that can carry a small amount of data. Because it is unspendable, nodes do not need to keep it in the UTXO set.
Key Takeaways
- Bitcoin Script is a simple, stack-based language that defines who can spend bitcoin.
- Outputs hold a locking script; inputs provide an unlocking script.
- Opcodes are the commands that make up scripts.
- Common script types include P2PKH, P2SH, SegWit, and Taproot.
- It supports multisig, timelocks, and payment channels, but is intentionally not Turing complete for safety.
Disclaimer: This article is for educational and informational purposes only and does not constitute financial, investment, legal, or tax advice. Cryptocurrency is volatile and carries significant risk, including the possible loss of your entire investment. Technical examples are simplified for learning purposes. Never attempt to create or broadcast custom scripts with real funds unless you fully understand them, as mistakes can result in permanent loss. Always do your own research and consult a qualified professional before making financial decisions. The author and publisher accept no liability for any losses arising from the use of this information.

Reader Comments (0)
Write a comment