The command get executed without the need of pressing Enter. This can be dangerous sometimes...
Why is that and how can I prevent it?
The command get executed without the need of pressing Enter. This can be dangerous sometimes...
Why is that and how can I prevent it?
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.
When you paste into a terminal, it's generally treated just as if you had typed in whatever characters you're pasting. The command running in your terminal can't distinguish between typed and pasted characters. So if you paste a newline, bash doesn't know it wasn't you pressing enter.
If you can't avoid pasting the trailing newline, or if you're not sure if there even is a trailing newline, there are a options for making pasting into bash a bit safer:
If you know you're pasting a single line, type #
before pasting. That way, if there's a newline at the end, the command will be commented out, and you can then go back up and edit it.
Note that if you paste in a multi-line string, this will only comment out the first line!
Safer, but a little more involved is to use bash's (seemingly little-known) edit-and-execute-command feature.
To invoke it:
CTRL-x CTRL-e
ESC v
This will open a text editor containing your current command-line. You can set which editor is used for this with either the $VISUAL
or $EDITOR
environment variables.
You can then paste into this editor, and then edit the command(s) before they are executed. Once you save and quit the editor, the command(s) saved by the editor will be executed.
If you want to abort you can either comment out or delete all lines in the editor. Alternatively, some editors let you exit with a non-0 status, in which case bash will not execute the command. In vim, for example, you can use :cq!
to exit with a non-0 status.
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 ;>
Why is that and how can I prevent it?
The why part was already covered in other answers. However, bash has a prevention mechanism that was not covered in the answers posted here, though it was presented on other stack exchange sites.
You may be looking for:
bind 'set enable-bracketed-paste on'
With this, pasting (as opposed to typing) will be detected, and the whole text will be entered at once on the prompt, including new lines, giving you a chance to edit them before executing the commands. This is similar to defaults used by zsh and other shells.
It is a feature of (GNU) readline
(≥7.0), a library used by bash and some other interactive shells (python, etc) to provide user-friendly interactive prompts. It can also be set as default for every readline program:
echo "set enable-bracketed-paste on" >> ~/.inputrc
From the manual (man 3 readline
):
enable-bracketed-paste (On)
When set to On, readline configures the terminal to insert
each paste into the editing buffer as a single string of
characters, instead of treating each character as if it
had been read from the keyboard. This prevents readline
from executing any editing commands bound to key sequences
appearing in the pasted text.
I've had the same issue and FIGURED IT OUT for this century, since it was asked 15 years ago and this came up first on a google search.
PowerShell and CMD both allow paste but when you use the arrow key to move up or you use your mouse to select text, it enters the character you typed at the end- not where your cursor was.
It does that, apparently to me, because the paste command (ctrl + V) automatically enters the "clipboard" and allows you to use the up/dn key to select something from your clipboard. Windows Clipboard Command (Windows Key + V)
Solution: Use the Left Arrow Key to navigate and type!
© 2022 - 2024 — McMap. All rights reserved.