Posted on 2026/02/04  
tagged bash  

bracketed paste

Bracketed paste is a feature in modern terminal emulators and Readline (used by Bash 5.1+) that protects users when pasting text, especially multi-line commands.

Key Aspects of Bracketed Paste:

Example _

To check if your terminal has bracketed pasted enabled

bind -v | grep enable-bracketed-paste

Paste Script _

I discovered the bracketed paste feature when wanting to copy and paste urls to download into a terminal.

Instead of just repeatedly pasting urls, I thought, why can't I just capture the paste event and automatically download?

example bracketed-paste.sh script

#!/usr/bin/env bash

function capture_paste {

    # Save current terminal settings
    local state=$(stty -g)
    # Turn off keyboard input echo
    stty -echo

    # Enable bracketed paste
    printf '\e[?2004h' >&2

    # Read the input
    IFS= read -r input

    # Disable bracketed paste
    printf '\e[?2004l' >&2

    # Strip bracketed paste markers if present
    input="${input#$'\e[200~'}"
    input="${input%$'\e[201~'}"

    # restore state
    stty "$state"

    echo "$input"
}

while true
do
    echo
    echo "Paste or type something:"
    result=$(capture_paste)
    echo "Result: $result"
done

write this to a file and make this script executable, run it, paste something, hit enter, and you'll get some output.