Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top