Quotes exercise - how to do ssh inside ssh whilst running sql inside second ssh?

view full story

http://unix.stackexchange.com – I want to run sql over ssh from php . Below command runs nicely ssh server1 "sudo -u db2inst1 sh -c '/opt/ibm/db2/V9.7/bin/db2 connect to RATIONAL; /opt/ibm/db2/V9.7/bin/db2 set schema Edumate; /opt/ibm/db2/V9.7/bin/db2 \"select * from edumate_settings\"'" Now I need to do ssh from server1 to server2 and then run "sudo -u db2inst1 .... " there. Is there any easy way how to deal with quotes if I add additional server to this scenario? So it will start like ssh server1 "ssh server2 "the rest is as above" Note the I need to run above code from php. (HowTos)