5

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.

9
  • 2
    As I commented on your previous question, don't past shell code inte the terminal. If you have a workflow that depends on you pasting shell code into a terminal, then you really seriously have to reconsider that workflow.
    – Kusalananda
    Jul 20 '18 at 9:42
  • Please edit your question and tell us what shell you are using and what terminal emulator.
    – terdon
    Jul 20 '18 at 9:46
  • Using bracketed paste as suggested in JoL's answer handles this better.
    – muru
    Jul 20 '18 at 9:46
  • This depends on the shell you are using. Using e.g. zsh will work as expected.
    – Anderson
    Jul 20 '18 at 9:47
  • 4
    @Kusalananda that doesn't change the fact that this is an interesting behavior though. Why would the comments change things? And we often paste a few lines of code into a terminal, that isn't always a bad thing.
    – terdon
    Jul 20 '18 at 9:48
0

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.

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.