0

I wish to ssh into a remote server and run a series of commands like (just some random commands)

touch foo.txt
echo 'bar' > foo.txt
cd ..
ls

and stay in that connection.

Someone suggested me using ssh -t $server "commands; bash". It worked but it landed me in a different directory than if I just do ssh $server. Any ideas why this happens?

(I'm studying existing code in a repo, so I wonder what is the mechanism that make this happen and why)

0

Any ideas why this happens?

Maybe your commands include cd. You're executing commands; bash, so bash starts in whatever directory commands end.

The obvious solution is to cd back just before you invoke bash. Try

ssh -t $server 'commands; cd "$HOME"; exec bash -l'

Notes:

  • Single quotes prevent expanding $HOME locally; in case you need commands to include locally expanded variables, mix the quotes (e.g. "commands with $var; "'cd "$HOME"; exec bash -l').
  • exec, because you don't need the parent shell on the remote side anymore (or do you?).
  • -l (equivalent to --login) because sole ssh $server spawns a login shell and you want your entire command to act alike. Bash as a login shell sources different file(s) than as non-login shell, starting with /etc/profile; the rules are somewhat complicated, see man 1 bash, look for INVOCATION. If one of these files performs cd to another directory, you'll get what you get even if commands don't change the directory. You didn't tell what the directories ("right" one and "wrong" one) are, so it's hard to even guess what the purpose of this "hidden" cd may be.

Or you can run commands in a subshell. This won't affect the working directory of the parent shell so bash will start wherever the parent shell starts:

ssh -t $server '(commands); exec bash -l'

You may prefer one solution or the other depending on what commands do. For example if commands modify the environment of their shell on the remote side then running them in a subshell or not makes a difference for the final bash.

7
  • Thank you for the reply! It's not the cd commands that changed the directory. The directory is different when before the cd. It seems a script is triggered/executed if I ssh normally but isn't triggered when I use ssh -t. Any idea why?
    – Kevin He
    Sep 20 '18 at 18:51
  • @KevinHe Does this depend on whether or not Bash is a login shell? Sep 20 '18 at 19:05
  • I'm only able to land in a certain directory if I only do ssh $server. If I do anything other than it, e.g. ssh $server 'pwd' or ssh -t $server 'pwd; bash', the working directory is different.
    – Kevin He
    Sep 20 '18 at 19:13
  • @KevinHe OK, please confirm "anything other than that" includes ssh -t $server 'pwd; bash -l'. Sep 20 '18 at 19:16
  • Oh bash -l landed me to the same place as I did at ssh $server. Can you explain it? Thank you!
    – Kevin He
    Sep 20 '18 at 19:21

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.