I regularly have to log onto remote hosts via SSH and I like to use the bash option set -o vi and I normally have to cd to a specific directory. Since these hosts are ephemeral (they are created and destroyed regularly) I can't log onto them and save these in a .bash_profile/.bashrc. So I was looking into how I can do this when I connect via SSH so I don't have to type the same commands each time I connect to a remote server. I've tried several ways to achieve this but none of them seem to work. Can someone help me get this command right? In a nutshell this is what I want to do.

  1. cd to a specific directory
  2. set the bash option set -o vi

Here's how I've tried to do it:

ssh root@remotehost.com -t "cd /data; exec bash --login -c \"set -o vi\""

I feel that this actually works but that the SSH session terminates because once the command set -o vi is executed the session disconnects with a message like:

Shared connection to remotehost.com closed.

Is there a way to keep the session open after executing these commands or is there another way to achieve what I want?


Some options:

ssh -t root@remotehost.com 'cd /data && exec bash --login -o vi'

(that works with all POSIX-like shells including ksh/zsh/dash... as -o is a standard option of the POSIX sh utility and vi happens to be one of the standard options. That command line is also compatible with shells of all major shell families including Bourne, csh, rc, fish so should work regardless of the login shell of the root user over there)


ssh -t root@remotehost.com 'cd /data && exec env SHELLOPTS=vi bash --login'

Actually, you don't want to use that second one. That means the $SHELLOPTS variable will be in the environment and affect all bash invocations (and the non-interactive ones will inherit the ones from interactive shells causing all sorts of problems).

Also beware that bash has two sets of options, one you set with -o/$SHELLOPTS and one you set with -O/$BASHOPTS).

  • first option worked perfectly – conorgriffin Nov 30 '17 at 12:16
  • Since it works for various shells, you could even make it slightly more general with ... exec $SHELL --login ... – Jeff Schaller Nov 30 '17 at 15:44
  • 1
    @JeffSchaller, --login is not standard; -o vi is only for POSIX-like shells (not csh, tcsh, rc, es, fish, Bourne...), not all even have a vi mode to start with. Here I'm under the impression the OP wants to start bash regardless of the login shell of the remote user. – Stéphane Chazelas Nov 30 '17 at 16:07

Make it two commands.

  1. Put the commands to be executed on the remote host into a file .bashrc.tmp.
  2. $ scp .bashrc.tmp root@remotehost.com:
  3. $ ssh <options> root@remotehost.com "bash --login --rcfile ${HOME}/.bashrc.tmp"

This way you don't even need to back up your original .bashrc.


To simplify jumping from a folder to another on your host, you can install apparix and set up bookmarks in the different places you need to jump, then use to folderX in apparix: https://micans.org/apparix/man/apparix.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.