2

I noticed that when I triple click (with mouse) to select text (or a command) from say a website, and then paste this into my terminal using Ctrl+Alt+v, then the command runs immediately (i.e. it automatically inputs Enter key), but if I select(with mouse) and copy the same command and use Ctrl+Alt+v to paste it into the terminal it does not auto-run the code but it rather just pastes it and awaits input from me before the code will run.

Which mechanism makes this so? Because I would rather first look at the command to be run before running it, but I noticed this will be done for me any time I use triple click to select a word.

2

Triple-clicking selects the whole line, which includes the line feed at the end1. A line feed character (Unicode U+0A) is what Enter inputs, so the shell interprets this to mean it should execute the command2.

When you click and drag to select the line, you normally don't select the line feed at the end, unless you drag the mouse down to the next line, or unless you select multiple lines (which of course include multiple line feeds between them).

The best workaround I know is to copy-paste the command into a text editor (e.g. Gedit), review it, then copy-paste it into the terminal.

Here are some harmless commands you can try copy-pasting in different ways:

echo 1
echo 2
echo 3
true true
echo hello world
echo "$(whoami)"

Footnotes

  1. From my understanding, a line is defined as a string terminated by a line feed.
  2. In my experience, most shells can't tell the difference between a string being typed and a string being pasted.
  • This is a good security practice too. – chaskes Aug 14 '17 at 2:49
  • @chaskes Is that just in case of malicious hidden text on a website, or other reasons? – wjandrea Aug 14 '17 at 6:59
  • mostly it's malicious hidden text, but it will also make obvious non-visible chars like newlines (say when copying a long sha256sum) – chaskes Aug 14 '17 at 16:20
3

enter image description hereenter image description here

Both of these were selected by dragging, not triple click. The difference is that, in the latter, I dragged to the next line. You can see the newline being represented as an extra space that does not actually exist in the text. With triple click, you'll always see the latter.

You might want to do either of:

  • edit the current command line in an editor (CtrlX CtrlE is bash's default shortcut), then paste in the editor.
  • switch to a shell like zsh, which does not execute a command on pasting even if it has newlines. This is pasted text, as yet unexecuted:

    enter image description here

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.