What is the cleanest way to ssh and run multiple commands in Bash?

前端 未结 12 2245
礼貌的吻别
礼貌的吻别 2020-11-22 09:14

I already have an ssh agent set up, and I can run commands on an external server in Bash script doing stuff like:

ssh blah_server "ls; pwd;"
         


        
12条回答
  •  故里飘歌
    2020-11-22 09:33

    For anyone stumbling over here like me - I had success with escaping the semicolon and the newline:

    First step: the semicolon. This way, we do not break the ssh command:

    ssh  echo test\;ls
                        ^ backslash!
    

    Listed the remote hosts /home directory (logged in as root), whereas

    ssh  echo test;ls
                        ^ NO backslash
    

    listed the current working directory.

    Next step: breaking up the line:

                          v another backslash!
    ssh  echo test\;\
    ls
    

    This again listed the remote working directory - improved formatting:

    ssh \
      echo test\;\
      ls
    

    If really nicer than here document or quotes around broken lines - well, not me to decide...

    (Using bash, Ubuntu 14.04 LTS.)

提交回复
热议问题