Bash – Execute bash commands over SSH while staying in interactive mode afterwards

bashssh

Probably it is a weird thing I would like to achieve but:
I would like to SSH to a remote host and then execute some bash commands like "alias" automatically to use them afterwards in an interactive mode. The remote host does not allow the personal environment.

Is this possible? Would "expect" do it? Or is there any other way to achieve this?
Basically I would like to have my bashrc without modifying any files on the remote host.

Best Answer

A solution based on Gilles's that doesn't require adding AcceptEnv on the server for LC_XXX:

ssh -t remote.example.com "
  exec bash --rcfile <(
    printf '%s\n' '$(
      sed "s:':'\\\\'':g" ~/.bashrc
    )'
  )
"

That's to use your local ~/.bashrc in the remote session. It takes advantage of the fact that everything in between single-quotes in bash is taken literally, so the only thing we need to worry about escaping are the inner single quotes themselves. We can safely quote bash-code by surrounding it with ', and substituting inner 's for '\''s.

If you want to add just a few aliases or something inline to the remote ~/.bashrc:

ssh -t remote.example.com "
  exec bash --rcfile <(
    printf '%s\n' '
      . ~/.bashrc
      alias ll=\"ls -l\"
    '
  )
"
Related Question