4

I'm logging in and out of a remote machine many times a day (through ssh) and I'd like to shorten a bit the whole procedure. I've added an alias in my .bashrc and .profile that looks like:

alias connect='ssh -XC username@remotemachine && cd /far/away/location/that/takes/time/to/get/to/;'

My problem is that when I write connect, I first get to the location in cause (on my local machine) and then the ssh connection takes place. How can this be? I've thought that by using "&&" the second command will be run only after the first one is successful. After the ssh command is successful, the .profile/.bashrc are loaded anew, before the second part of the alias is successfully executed?

mannaroth
  • 1,473
  • 3
  • 17
  • 38

2 Answers2

2

For the ssh specifically, you're looking for the following:

ssh -t username@remotemachine "cd /path/you/want ; bash"

Using "&&" or even ";" normally will execute the commands in the shell that you're currently in. It's like if you're programming and make a function call and then have another line that you want to effect what happens in the function-- it doesn't work because it's essentially in a different scope.

Greg
  • 1,053
  • 7
  • 8
  • Thanks, I've tried adding the -t option to ssh like: `alias cmd='ssh -tXC username@host "cd /location/; bash"'` and it seems to be now working! It's just that the .bashrc is somehow loaded twice; adding a line at the end of that file: echo ".bashrc loaded" gives two such messages after connecting through ssh to that remote machine. I guess that should not produce any trouble. – mannaroth May 11 '13 at 19:09
-3

For sequence of commands :

Try this (Using ;) :

alias cmd='command1;command2;command3;'

Use of '&&' instead of ';' - The && makes it only execute subsequent commands if the previous returns successful.

Debaditya
  • 2,419
  • 1
  • 27
  • 46
  • I have actually tried that (";") first but it didn't work. Then I presumed that maybe I should make the "cd" command run only after a successful confirmation of connection from ssh ("&&"). – mannaroth May 11 '13 at 19:01