Set current working directory on ssh

11

4

I am trying to ssh from one machine to another within a Python script (Nautilus script). It works with the following:

ssh -t user@server "cd /home/some/dir ; csh"

However I don't like the idea of hardcoding the shell type.

Is there a way to set cwd within the ssh command without the need to hardcode the shell type?

umpirsky

Posted 2011-05-30T13:22:15.633

Reputation: 255

http://stackoverflow.com/questions/2593570/how-to-make-ssh-go-directly-to-specific-directory – Ciro Santilli 新疆改造中心法轮功六四事件 – 2016-08-15T10:05:36.933

Technically it was a python script ;-) – Johnsyweb – 2011-05-30T10:18:35.707

Answers

8

Invoke $SHELL instead.

ssh -t user@server 'cd /home/some/dir ; exec "$SHELL"'

Ignacio Vazquez-Abrams

Posted 2011-05-30T13:22:15.633

Reputation: 100 516

9

You can do this like so...

ssh -t user@server "cd /home/some/dir ; bash"

Where bash is your desired shell.

Johnsyweb

Posted 2011-05-30T13:22:15.633

Reputation: 2 324

Thanks, it works. Is there a way to auto-detect or ommit shell, so it go to default? I don't like the idea to hardcode it. – umpirsky – 2011-05-30T11:21:39.677

1@umpiresky: I don't have an Ubuntu box on which to test this, but on *BSD I can do: ssh -t user@server "cd /home/some/dir ; login user" (where user is the username that you have provided to ssh). If your remote host has the same setup as localhost you could use ${SHELL} instead. YMMV. – Johnsyweb – 2011-05-30T11:30:21.617

@Johnsyweb Nope, login user ddn't work for me. I'm connecting from Ubuntu to FreeBSD, but it would be nice to work for any server. That's why I don't like the idea to hardcode shell type (csh in my case). – umpirsky – 2011-05-30T12:05:47.660

login user works for me on FreeBSD. – Johnsyweb – 2011-05-30T12:06:49.090

Hm, strange, for me terminal crashes after I provuide password. – umpirsky – 2011-05-30T12:16:24.523

@umpiresky: You included the '-t'? – Johnsyweb – 2011-05-30T12:24:56.927

@Johnsyweb Yes. – umpirsky – 2011-05-30T12:37:15.533

@umpiresky. Then I am stuck. I suggest migrating the question to http://askubuntu.com/ or http://unix.stackexchange.com/ . Someone there will know!

– Johnsyweb – 2011-05-30T12:40:46.467

1Hm, maybe, yes. But we already have a working nautilus script, lets just make it better. Thank fou your valuable help. – umpirsky – 2011-05-30T12:42:51.900