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.

terdon
  • 234,489
  • 66
  • 447
  • 667
pLumo
  • 22,231
  • 2
  • 41
  • 66
  • 2
    [As I commented on your previous question](https://unix.stackexchange.com/questions/457235/pasting-multiple-commands-into-terminal-stops-at-user-input#comment830703_457235), _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 09:42
  • Please [edit] your question and tell us what shell you are using and what terminal emulator. – terdon Jul 20 '18 at 09:46
  • Using bracketed paste as suggested in [JoL's answer](https://unix.stackexchange.com/a/457309/70524) handles this better. – muru Jul 20 '18 at 09:46
  • 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 09:48
  • This depends on the shell you are using. Using e.g. `zsh` will work as expected. – Anderson Jul 20 '18 at 09:47
  • @Anderson Changing one's shell to get pasting to work as expected would be a bad solution as the code that the user is pasting may well be particular for the shell they are currently using. – Kusalananda Jul 20 '18 at 09:53
  • I updated the Q with the information – pLumo Jul 20 '18 at 09:54
  • @Kusalananda I was talking about shell functionality in terms of pressing arrow up to reuse a previous command in the history. – Anderson Jul 20 '18 at 09:56
  • Nice catch! I can reproduce with bash-4.4, but not with the developer branch. You still get a two-line command there though (without the comment); unlike with the comment-free version where a ';' gets inserted. You might want to report this discrepancy to the developers. – egmont Jul 20 '18 at 12:45

1 Answers1

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.

nethero
  • 164
  • 5