Wednesday, March 30, 2011

How can I run long pipe lines of commands on a remote host via SSH without escaping all the meta characters?

$ ssh kramer <<EOF
ps -ef | grep http | awk '{print \$NF}'
EOF

The only tricks here are the use of a bash here document, and the fact that the command line is typed directly into ssh’s STDIN so there’s no need to escape things like pipes and semi-colons, etc. However, notice that you do still need to escape dollar signs because they’ll still be interpreted as shell variables.