Bash – Pasting into terminal including comments issue

bashclipboardterminal

This is somehow a follow-up to my last question: Pasting multiple commands into terminal stops at user input


Pasting the following to the terminal works as expected:

(
echo test1
# some comment
echo test2
)

But pressing arrow up to repeat that command gives the following obviously wrong command:

( echo test1 echo test2; )

Pasting the same without the comment and pressing arrow up gives the expected command:

(echo test1; echo test2)

An easy workaround is to add ; before each line with a comment.

But why is this the case ? Is it a bug ?

I use bash. Same behavior in gnome-terminal and tilix.

Best Answer

The problem is not really where you paste to, but where from are you are pasting. Additionally if you are working with Windows, you'll never know what was copied and what will be pasted (white spaces, weird line endings, xml like formatting and all this stuff that makes CP operations a pain in win10).

If you need to paste a code that you want to run in your terminal then fc is your best friend. What it does, it opens your editor (configured in $EDITOR variable) e.g vim and allows you to format your last command end execute it on exit. This way you can paste whatever you need and modify it before running it also helps sanitizing your input. Very good if you need to make a lot of changes to your last command.

As said by someone in the comment, pasting code into terminal for direct execution is never good idea and can cause costly mistakes.

Related Question