9

When I paste into my terminal session the shell immediately executes the command without me pressing the enter key.

I really don't know how to disable that behaviour.

I'm using the preinstalled terminal on MacOS Yosemite.

  • 9
    It does that only, of course, if the paste buffer contains a newline. This has (almost) nothing to do with bash. (It concerns bash only if and as far as it uses the readline library.) – Peter A. Schneider Sep 14 '16 at 14:47
  • ... and yet bash (with the help of readline) has a fix. – alexis Sep 14 '16 at 16:27
15

With bash 4.4 or newer and in terminals that support bracketed paste a la xterm, you can do:

bind 'set enable-bracketed-paste'

(or add set enable-bracketed-paste to your ~/.inputrc)

That will cause the copy-paste buffer to be inserted at the prompt instead of the characters in it to be interpreted as if typed (you could still have problems if that buffer contains characters like ^C, ^Z and your terminal emulator doesn't filter them out).

zsh does that by default since version 5.1.

For other shells or terminals, see also: How can I protect myself from this kind of clipboard abuse?

  • I'll look right into it – Opaldes Sep 14 '16 at 15:38
  • booth ways doesnt seem to work – Opaldes Sep 14 '16 at 15:48
  • @Opaldes, what did you try? With what version of what shell and on what terminals? I beleive OS/X also ships with xterm, you could try that instead. Have you tried the perl script at the linked question? – Stéphane Chazelas Sep 14 '16 at 16:08
  • 1
    @Opaldes, according to Pasting code into terminal window into vim on Mac OS X OS/X Terminal supports bracketed paste since OS/X Lion. So it would seem all you need is a recent enough version of zsh or bash. With older zsh versions, that can also be achieved via configuration. – Stéphane Chazelas Sep 14 '16 at 16:12
  • 1
    We can thank Daniel Colascione for adding this useful feature to Bash/Readline: gnu-bash.2382.n7.nabble.com/… – Anthony Geoghegan Sep 15 '16 at 8:52
6

This happens when you paste a "newline" character. For example, if you're pasting multiple lines or you unknowningly copy a carriage return at the end of the line. AFAIK there's no way to actually disable this behaviour but there are ways to mitigate it.

The easiest way is to just copy one line at a time and miss off the last character, manually adding it afterwards.

Or, as the_velour_fog points out below, you could also paste it into an editor that shows non-printed characters and reselect without the newline.

There's one or two workarounds floating around for GNU/Linux distros but no idea if the concept could be ported over to MacOS. Regardless, take a look here and see if there's a similar file in mac that you could alter. Unfortunately, I doubt it.

  • I just paste it into an editor that shows non-printed characters, then reselect without the newline – the_velour_fog Sep 14 '16 at 10:42
  • Good suggestion, edited to include. – I_GNU_it_all_along Sep 14 '16 at 10:50
  • good to know that explains that it happens not all the time. I upvoted your answer but it is more a workaround as a solution to me. – Opaldes Sep 14 '16 at 11:08
  • It's a pain but can be useful when copying a series of commands. – I_GNU_it_all_along Sep 14 '16 at 11:09
  • My "solution" is also sort of a hack workaround, but it's faster than selecting separately, IMO. – Peter Cordes Sep 14 '16 at 14:12
5

Before pasting, type a single-quote, then paste. control-c and up-arrow to edit it as one giant line.

double-quotes also work, in case the text contains single but not double quotes. It's ok if it contains $ or other things that expand inside double quotes, because you're not actually running the quoted giant line as one command.


You can't actually edit the whole thing right there, because newlines do start a continuation prompt (not the official terminology) instead of making one big long line.

given these 4 lines:

echo
ls
foo
bar

pasting as I suggest will result in this on your screen:

$ 'echo 
> ls
> foo
> bar        <or cursor here if you didn't include a trailing newline
> [cursor here]

You can then press control-C and up-arrow. (control-C destroys everything on the same line as the cursor, so press return first if there's useful text there.)

An alternative is to close the quote and press return, then up-arrow that.

Anyway, after up-arrow

$ 'echo
ls
foo
bar

control-a and remove the leading '. Use control-left and control-right to move quickly through the paste block (by words). Or if your terminal setup doesn't support control-arrow-keys, alt-b and alt-f.


The literal newlines do actually separate commands, and these 4 lines will go into your command history separately. You might want to change them to ; or &&. (I don't know a trick for that, just use key-repeat for forward or backward-word to get there quickly and do it manually.)

2

This happens because your paste ends with a newline, and terminal emulators basically feed the paste to bash character by character. In this process, bash doesn't know human typist from a machine really.

I usually just pay extra attention when doing copy-pasting that it doesn't include a newline, but there's one trick that you can try: before paste, in bash, type in C-x C-e. It should launch $EDITOR with an empty file. Whatever you save in that file and exit, bash will execute. So you can now paste your command to the editor, possibly manipulate things further and then run them all in one go.

-1

I am NOT related to the company, I am a very satisfied customer. Try this terminal program: http://www.emtec.com/zoc/index.html. There is also a Mac version available.

If I violate with this answer any SE rule please let me know and I delete the answer.

Edit:

When I understood the other answers correctly this problem may be caused by the terminal program. The ZOC terminal program is a professional software which is perfectly designed for the work. It pastes the text you selected without any additional character. You can log your sessions in a separate text file. And lot more

Update:

The terminal program has an option Paste without final 'newline'. May be this helps.

  • 3
    Please at least say how it answers the question – Stéphane Chazelas Sep 15 '16 at 8:18
  • If you paste a string that contains newline or carriage-return characters at the prompt of a shell running in that terminal, what happens? If it doesn't cause the shell to execute the commands straight-away (as asked here), how does it do it? Does it remove those characters from the selection? Does it insert a ^V character before to escape it for bash? – Stéphane Chazelas Sep 15 '16 at 9:54
  • 2
    As such, it looks more like spam (though I'm sure it wasn't your intention to spam this site). It doesn't answer the question and is just advertisement for a related commercial product. – Stéphane Chazelas Sep 15 '16 at 10:09
  • 2
    @AlBundy, find out if your favorite terminal client solves this problem by trying out the exploits discussed in security.stackexchange.com/q/39118 (mentioned in Stephane's answer), and the links you'll find there. If it protects you, you have good grounds to recommend it (do provide the details of your tests in your answer.). – alexis Sep 15 '16 at 11:58
  • Usually the problem is that what you selected does include a newline. How is "pastes the text you selected" any different from the normal behaviour of any other terminal emulator? – Peter Cordes Sep 16 '16 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.