Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

This question reminded me of one of my own. I want to create an alias that will ssh to a machine, runs a command (in this case, cd to a provided path) and then gives me a shell. I've run a hundred variations of this:

$ ssh user@machine 'cd /some/path/ && bash -i -l'

which is about the best I can do. It gives me a bash prompt in the right directory, but without job control and without the ability to use arrow keys (and some other shortcuts). Is there an easy way to do this?

I can't do this easily at the end of my bashrc or profile, because the path I want to go to will vary (I'll pass it as an argument to the alias) and occasionally, I'll even ssh to that machine without wanting to go anywhere weird at all -- home directory is fine.

share|improve this question

4 Answers 4

up vote 12 down vote accepted

Try giving ssh the -t option to have it allocate a terminal.

share|improve this answer
    
Oh my god, that worked. You're my hero. –  Plutor Aug 4 '11 at 15:09
4  
You probably have two shells running at this point. If that's a concern, you might preface the call to bash with exec. I think that will work. –  wfaulk Aug 4 '11 at 15:14

Look at the sshd man page, Authorized_keys File format section. Inside it has the option command="command" which is what to do when that key connects. The original included command for the connection is included in a variable.

You might be able to set this command to run bash with a startup script that checks the original command value and runs it, then leaves you in an interactive shell.

share|improve this answer
    
+1 I love this! –  rlduffy Aug 4 '11 at 23:55

This is going to be different because cd is not a command, but rather a shell builtin that changes the programs current working directory.

I'm afraid I never really found a good way to do this -- but maybe that will help clarify some confusion.

share|improve this answer
    
Yes, but the cd command is handed off to a shell by sshd, and CWD is passed to child processes. –  wfaulk Aug 4 '11 at 15:16

I think the best place to do your cd /some/path/ is in your .bashrc on the remote machine.

share|improve this answer
    
The problem is that /some/path/ isn't always the same and sometimes I ssh to the machine and want to just be in my home directory like normal. I'll add that info to the question. –  Plutor Aug 4 '11 at 14:41
    
taking the manpage literally, I think it's either a command, or a proper interactive shell –  Sgaduuw Aug 4 '11 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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