Say, I have a "Quick&Dirty" Perl script in a gui text editor, and I start the Perl interpreter in a terminal window which is running Bash. I can copy-paste the Perl script to the terminal & press CTRL-D to execute it with Perl. Perl will interpret the script and execute it.

Sometimes, there is a typo in the script, which makes Perl print a FATAL ERROR and exit, but the remaining copy-paste text is given to the Bash shell which tries to execute that; Either that fails (lucky case) or executes but will Do something Different (unlucky case).

Eg : start Perl in Bash Prompt , copy-paste a script of 5 lines : lines 1 & 2 are fine , but line 3 makes Perl exit abnormally , hence line 4 & 5 are executed by Bash.

I am using Perl & Bash only for example; Problem can happen with many other interactive tools ( Eg Python, fdisk ) and other shells ( Eg zsh, csh )

Is there any way to inform the shell that this text is a copy-paste input and can be ignored ?
Something like "If faster than user can normally type, then Ignore" ?
Or something like "If Process finished, then flush input buffer before reading next shell input" ?

[[ Some tools do not have this problem, Eg MySQL cli will never exit on improper input. ]]


3 Answers 3


This is not an answer, but maybe it's an acceptable work-around:

alias p='perl; echo hit control-d again; cat > /dev/null'

Then, if your perl script exits prematurely, you'll harmlessly paste the remainder to /dev/null; if the perl script succeeds, you'll see your friendly reminder and hit control-d to exit the cat catcher.

  • Brilliant Solution !!
    – Prem
    Commented Feb 6, 2016 at 18:31
  • 1
    let's settle for "nice workaround" :) It is a flexible enough idea to adapt for any other tool (python, fdisk, etc). You could confuse yourself by getting fancy with alias perl=command perl; echo ...cat... so that you don't have to remember to type 'perl', but ...
    – Jeff Schaller
    Commented Feb 6, 2016 at 18:39
  • Out of curiosity : Did you come up with a solution (I mean "workaround") right now ? Or were you a victim of this problem earlier ?
    – Prem
    Commented Feb 6, 2016 at 18:43
  • 1
    I've written quite a bit of command-line perl, but have never suffered a copy/paste overflow. Since I don't have a good shell-centric solution, the bit bucket came to mind!
    – Jeff Schaller
    Commented Feb 6, 2016 at 18:55

In addition to the "solution/work-around" by Jeff Schaller , the proper solution seems to be to make the environment aware of copy-paste, both from sender side and from receiver side, which is known as "bracketed paste mode".
From https://cirw.in/blog/bracketed-paste , here is a short snippet to explain a little more :

In summary:

  1. Enable bracketed paste: printf "\e[?2004h"
  2. Wait for paste to start: you'll see \e[200~ on STDIN.
  3. Wait for paste to stop: you'll see \e[201~ on STDIN.
  4. Disable bracketed paste: printf "\e[?2004l"

It is not yet fully available for Bash, so I will be accepting the answer by Jeff Schaller, which works fine for me.


Instead of pasting into the terminal, have the command read from the clipboard. You can use xclip or xsel to print the clipboard content (or conversely write their input to the clipboard).

xsel | perl           # automatic selection (click+drag, middle click)
xsel -b | perl        # manual selection (Ctrl+C, Ctrl+V)

This only works if the shell in the terminal has access to the X server. It doesn't work, for example, across an SSH session that doesn't forward the X11 connection.

On OSX, use pbpaste to retrieve the pasteboard content.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .