1

I am copying and pasting sequences of shell commands into my terminal emulator, and I am confused by the behaviour.

  • When I paste the single line ssh user@remote "echo foo"; echo baz the computer does what I expect. It runs the SSH command and then echoes something after I have finished my SSH session.
  • When I paste the same thing as a multiple-line command, however, this does not happen.
    ssh user@remote "echo foo"
    echo baz
    I still get the SSH session, but the echo baz completely disappears. No command is run.
  • This seems to be something to do with SSH, at least. Because replacing SSH with something else yields the behaviour that I was actually expecting. Both commands run when I paste this, for example:
    echo foo
    echo baz

So what is happening? Where is the pasted input going? Why is the echo command not executed by anything? How is this related to SSH?

(By the way: I know of Ctrl+X, Ctrl+E method, but sometimes, especially when using terminal emulators on Windows, this won't work out of the box.)

4
  • The conclusion that you have jumped to, upon which you have then based the question, is wrong.
    – JdeBP
    Jun 25, 2018 at 16:52
  • Do you mean specifically, that I'm referring to ssh as the source of this behavior? It might be one of the cases, and since I didn't search for any other, I've decided to leave it as an example. Or do you refer to "copy-pasting" instead of "evaluating pasted"?
    – mjktfw
    Jun 25, 2018 at 17:05
  • You've jumped to the conclusion that stuff is not being pasted, and then asked why pasting breaks. It is not actually the case in the first place that the input is not being pasted. So this question is based upon a false assumption from the title onwards. Where is this pasted input going? is by far a better question.
    – JdeBP
    Jun 25, 2018 at 17:15
  • There you go. The answer to this is actually fairly straightforward, Let's see whether anyone answers it.
    – JdeBP
    Jun 25, 2018 at 17:30

1 Answer 1

1

Try pasting this:

ssh user@host 'read foo; echo ">>$foo<<"'
echo bar

You will see >>echo bar<< as output from the echo on the server.

This means that ssh will eat the pasted data on its standard input, as if you had done

echo 'echo bar' | ssh user@host 'read foo; echo ">>$foo<<"'

The solution to this, apart from never pasting shell commands into the terminal to run them, is to use ssh -n, which will redirect /dev/null into ssh:

ssh -n user@host 'echo foo'
echo bar

Unfortunately I don't know the mechanics of pasting, so I can't say anything clever about why the pasted data ends up on the standard input stream of ssh, but I'm guessing it has to do with the stream being inherited from the interactive shell (which reads commands from standard input).


Using ssh -n is also the solution (or at least a solution) to the issue of

while read something; do
    ssh user@host 'somecommand'
done <inputfile

Here, the loop will only run once since ssh eats the data from inputfile.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .