Bash pass multiple commands to SSH and make readable I have a script that sends multiple commands after logging in a remote machine via SSH.

Problem is, it's barely readable. I've tried quoting everything with "", but it's terrible (everything appears in pink), and here documents are just as bad (everything appears in gray). I'm using Gedit as an editor, but I tried Emacs as well.

Can I make the editor parse the remotely executed code as code?

#!/bin/bash

function remoteExec()
{
    echo "remote execution starting"
    ssh -n "$1" &lt;&lt; 'EOF'
#the following code is _not_ highlighted by real editors
currIt=\"$2\" #I hope this parameter is passed correctly
while [ "$currIt" -lt 5 ]; do
    echo "hello"
    currIt=$(($currIt + 1))
done
EOF
}


I'd rather not create a separate script just for these lines, because then I'd have to copy it on the remote machine, or pipe it, and I have arguments too.

EDIT: as Glenn noticed, as far as highlighting goes, there isn't much to do. I'm open to solutions putting the code in a separate script. I need to transfer the code to the remote host then, and pass it the necessary arguments.
You're quoting the heredoc terminator, so $2 will certainly not be expanded

When you want to pass multiple commands to ssh, wrap them as a single script to an interperter, thus ssh sees one single command.

Lots of whitespace for readability.

And yes, not much opportunity for syntax highlighting.

Since you need variable expansion within the script, you need to worry about quoting variables you want to be expanded on the remote host. I changed the while loop to a for loop to minimize the number of $-signs to escape.

If the script you want to send is more complex, be careful about how you use single and double quotes.

function remoteExec()
{
    echo "remote execution starting"
    ssh -n "$1" &lt;&lt; EOF
        bash -c '
            for (( currIt="$2"; currIt &lt; 5; currIt++ )); do
                echo "hello"
            done
        '
EOF
}




Given an arbitrary number of args:

function remoteExec()
{
    local host=$1
    local startIt=$2
    local quotedArgs
    shift 2
    for arg in "$@"; do
        quotedArgs+="\"$arg\" "
    done

    echo "remote execution starting"
    ssh -n "$host" &lt;&lt; EOF
        bash -c '
            for (( currIt="$startIt"; currIt &lt; 5; currIt++ )); do
                echo "hello"
            done
            args=( $quotedArgs )
            printf "%s\n" "\${args[@]}"   # escape this sigil
        '
EOF
}

remoteExec host 3 arg1 arg2 "this is arg3"