Why sometimes, when I insert a command into my bash prompt, it starts even if I don't press Enter?

The command is executed without having to press Enter. Sometimes it can be dangerous ...

Why is this and how can I prevent it?

+6
bash copy-paste
source share
3 answers

Because you insert a new line character with it. Sometimes this can be useful, for example, you can copy / paste several commands at once (long multi-line scripts).

Well, it never crossed my mind to prevent this particular behavior. This is normal and expected. WYPIWYG - what you insert is what you get.

+12
source share

You insert one or more newline characters. Besides just copying and pasting newlines, there are a few things you can do to get around this:

  • For single-line commands, first enter "#", so the command will be commented out. Then you can go back and edit it.

  • Use the bash (seemingly little-known) edit-and-execute-command function . To call it, you can press CTRL-x CTRL-e if you use emacs (the default) or ESC v if you use vi keybindings. This will invoke a text editor containing the current command line. Then you can paste into the editor and edit the command. After you save and close, the commands saved by the editor will be executed (if you want to interrupt any comment from all lines or completely clear the buffer). You can set which editor is used with the FCEDIT or EDITOR environment EDITOR .

+9
source share

A quick way to prevent execution is to enter the comment character #, and then insert the command.

I often do this because I'm a fat finger when copying and capturing extraneous characters.

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

--- reply to comment

You are right, this only works for single-line commands. If the clipboard is multi-line, you can transfer the clipboard data through sed.

Stupid bash trick number 4 million and one:

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

will do the following:

for i in *.JPG;

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

done;

in it:

# for i in *.JPG;

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

# done;

Which is really not worth the effort, but a curious pleasure; >

+3
source share

All Articles