views:

281

answers:

3

The command get executed without the need of pressing Enter. This can be dangerous sometimes...

Why is that and how can I prevent it?

+11  A: 

Because you paste new line character with it. It's sometimes can be useful, for example, you can copy/paste many commands (long multi-line scripts) at once.

Well, It has never occurred to me to prevent this particular behavior. It's normal and expected. WYPIWYG - what you paste is what you get.

alex vasi
So if the new line character is at the end of the line, one work around would be copying all but the last character?
Alberto Zaccagni
Yep, that should be obvious.
alex vasi
+2  A: 

A quick way to prevent execution is to type the comment character #, then paste the command.

I often do that because I fat-finger when copying and grab extraneous characters.

When you paste after the comment character, then the command is in the history buffer and you can edit it, uncomment it, and run it.

--- reply to comment

You're right, this only works for single-line commands. If you have a multi-line one in the clipboard, you can pipe the clipboard data through sed.

Stupid bash trick # 4 million and one:

prompt:$ xclip -o -selection clipboard | sed --regexp-extended 's/^(.*)$/# \1;/'

will turn this:

for i in *.JPG;

do echo mv $i ${i/.JPG/.jpg};

done;

into this:

# for i in *.JPG;

# do echo mv $i ${i/.JPG/.jpg};

# done;

Which is really not worth the effort, but kinda fun ;>

Ron Ruble
Great for single-line commands, but if it's a multi-line then the first instruction won't get executed, but the rest still will.
Andy
+2  A: 

You're pasting one or more newline characters. Other than simply not copy and pasting newline characters there are a few things you can do to work around this:

  • For single-line commands, type "#" first, so the command will be commented out. You can then go back up and edit it.

  • Use bash's (seemingly little-known) edit-and-execute-command feature. To invoke it you can hit CTRL-x CTRL-e if you use emacs keybindings (the default) or ESC v if you use vi keybindings. This will invoke a text editor containing your current command-line. You can then paste into the editor, and edit the command. Once you save and quit the command(s) saved by the editor will be executed (if you want to abort either comment out all lines or completely clear the buffer). You can set which editor is used with either the FCEDIT or EDITOR environment variables.

Laurence Gonsalves