I have a server I have to ssh into with many commands which feature the use of variable assignment and consequently a lot of quotation marks. If we consider the script:ssh user@host  kinit -k -t /path/to/dir/`whoami`.`hostname -s`.keytab `whoami`/`hostname -f`@host ;set -xtouch health_check.log ;echo \Starting HDFS health check\ > health_check.log ;echo \/path/to\ >> health_check.log ;hdfs dfs -du -h /path/to &>> health_check.log ;echo \**************************************************************************/path/to/dir\ >> health_check.log ;hdfs dfs -du -h /path/to/dir &>> health_check.log ;subject=\HDFS Health Check\ ;from=\noreply@example.com\ ;recipients=\user@email.com\ ;mail=\subject:$subject\nfrom:$from\nContent-Type: text/html\nMIME-Version: 1.0\n\n$(cat health_check.log)\ ;echo -e $mail | sendmail \$recipients\ ;set +xrm health_check.logWhen I run the script I get this debug output:++ subject='HDFS Health Check'++ from=noreply@example.com++ recipients=user@email.com++ mail='subject:\nfrom:\nContent-Type: text/html\nMIME-Version: 1.0\n\nStarting HDFS health check /data/gftocon'++ echo -e++ sendmail ''Notice the empty strings where my variables should be. Why has escaping the quotation marks not worked?
Multiple commands with quotations after SSHing
bash;shell script;ssh;quoting
That's because the script is double quoted: your shell is substituting the variables before launching the ssh command. You could add some extra backslashes, or use a quoted heredoc like this:ssh user@host  <<'END_COMMANDS'# ...............^............^ these quotes make the whole document single-quotedkinit -k -t /path/to/dir/`whoami`.`hostname -s`.keytab `whoami`/`hostname -f`@hostset -xecho Starting HDFS health check > health_check.log echo /path/to >> health_check.log hdfs dfs -du -h /path/to &>> health_check.log echo **************************************************************************/path/to/dir >> health_check.log hdfs dfs -du -h /path/to/dir &>> health_check.log subject=HDFS Health Check from=noreply@example.com recipients=user@email.com headers=subject:$subjectfrom:$fromContent-Type: text/htmlMIME-Version: 1.0{ echo $headers; echo; cat health_check.log; } | sendmail $recipients set +xrm health_check.logEND_COMMANDSI think that's easier to maintain.