"here-docs" save the day frequently. It's not as hard as it seems:
ssh me@myserver "/bin/bash -s -- arg1 arg2" << 'EOF'
echo " | Now I am running in the remote shell, my pid is $$"
echo " | Variables need no special quoting, e.g. if I print the first arg..."
echo " | you will see arg1($1) in stdout."
echo " |"
echo " | My hostname is $(hostname), in case you doubt it!"
echo " | If you want to get fancy you can even more here-docs within this here-doc:"
cat <<'XXEOF' > ~/some_script
#!/bin/bash # How about a comment for our generated script?
echo " >> Welcome to $(hostname), I was generated from an ssh command line"
echo " >> Inner date is $(date -Iseconds)"
echo " >> If we wanted to, we could go to a 3rd level of nesting. Did you ever"
echo " >> see the movie 'Inception'?? Same thing: it's here-docs all the way down"
echo " >> "
XXEOF
chmod +x ~/some_script
echo " | Outer date: $(date -Iseconds)"
sleep 2
~/some_script
EOF
Produces this output:
| Now I am running in the remote shell, my pid is 115798
| Variables need no special quoting, e.g. if I print the first arg...
| you will see arg1(arg1) in stdout.
|
| My hostname is myserver, in case you doubt it!
| If you want to get fancy you can even more here-docs within this here-doc:
| Outer date: 2025-01-14T09:03:47-05:00
>> Welcome to traind-pw-679, I was generated from an ssh command line
>> Inner date is 2025-01-14T09:03:49-05:00
>> If we wanted to, we could go to a 3rd level of nesting. Did you ever
>> see the movie 'Inception'?? Same thing: it's here-docs all the way down
>>