Bash – Single command to login to SSH and run program

bashopensshshellssh

Is there a way to structure a single command to login via SSH to a remote server and run a program on the remote login shell?

In the OpenSSH manual, it reads "If command is specified, it is executed on the remote host instead of a login shell." So, for example, ssh user@server mail will login to the remote server, display the mailbox status, and then return you to the local shell. Is there a way to stay on the remote shell after displaying the mail status?

Furthermore, ssh user@server [command] does not seem to work if the command is a program, for example vim or mutt. Is there a way to login to the remote shell and run a program while staying in the remote shell during the program and after exiting the program, only finally exiting upon a specific logout command (just like in a normal SSH session)?

I would eventually like to be able to put such a command as an alias in the local .bashrc, so that it could be run quickly when desired. An example would be to login via SSH to a remote server and open mutt on the remote server to read or send email.

Best Answer

Have you tried ssh -t user@server "mail && bash" (or replace bash with whatever shell you like)?

The -t is necessary in order to create a pseudo-tty for bash to use as an interactive shell.

Related Question