bashshellsshremote-access

Write SSH command over multiple lines


I'm trying to remote login to a shell and execute a bunch of commands on the shell. But to make it more readable, I'd like to place my code over multiple lines. How should I be doing this?

ssh -o <Option> -x -l <user> <host> " $long_command1; $long_command2; ....  "

Thanks!


Solution

  • ssh is in fact just passing a string to the remote host. There this string is given to a shell which is supposed to interpret it (the user's login shell, which is typically something like bash). So whatever you want to execute needs to be interpretable by that remote login shell, that's the whole rule you have to stick to.

    You can indeed just use newlines within the command string:

    ssh alfe@sweethome "
      ls /home/alfe/whatever
      ping foreignhost
      date
      rm foobar
    "