I want to run some commands each time when I log in to a remote system. Storing commands in .bashrc on remote is not an option.
What is the proper way to escape the escape chars inside of quotes in bash script for ssh? How can I write each command in new line?
My script
#!/bin/bash
remote_PS1=$'\[\033[01;32m\]\u@\[\033[03;80m\]\h\[\033[00m\]:\[\033[01;34m\]\!:\w\[\033[00m\]\$ '
ssh -t "$@" 'export SYSTEMD_PAGER="";' \
'export $remote_PS1;' \
'echo -e "set nocompatible" > /home/root/.vimrc;' \
'bash -l;'
didn't work.

sshto avoid any escapingremote_PS1is only defined in the local shell, not the remote one (and you shouldn't use$whenexporting it).remote_PS1in local script but I tried to use it in third line.$variablereference, just remove the single-quotes and pass the command on literally; the remote shell will then see it as an unquoted variable reference, try to expand it, get nothing, and therefore run the commandexport ;, which is invalid. Putting that line in double-quotes would cause a different problem; the local shell would expand the variable, and pass it to the remote shell asexport <bunchofgibberishescapes> ;with no variable name. You have to think carefully about exactly what's happening where."export PS1='$remote_PS1';" ` -- the double-quotes will allow$remote_PS1` to expand, but the single-quotes will protect that expanded value when it gets to the remote shell. Yes, this really is complicated (and if the value had any single-quotes in it, it'd be even more complicated). And as I suggested in the other answer, you can debug it by replacingssh -t "$@"withecho, to see what'd be sent to the remote shell (i.e. the command after the local shell processes it, but before the remote shell does).