0

NOTE: This is NOT about how to ssh into a certain directory (that question has been answered many, many times). This is about the proper use of variables in ssh'ing aliases.

I have a directory that I'm working inside of called /the/full/path/to/my/local/working/directory

and on my server at work (that I share with others so no rerouting via cd commands in .bash_login or .bashrc allowed on the server), I have a directory called ~/myname/working/directory. Since these two (after local and myname in the pathname) are mirror images of each other, I would like to write a line in my local .bashrc so that when I ssh in, it detects what directory I'm in on my local machine and sends me to the mirror.

So far I have:

GRND=$(cut -d/ -f8- <<< "$PWD")
#<-This returns 'working/directory/'
alias zxc="ssh -t xxx@xxx.xxx.xxx 'cd myname/\"${GRND}\";bash'"

For some reason it's not liking how I'm using the variable GRND. I tried messing with single quotes and double quotes and even added backslashes so I could nest double-quotes but to no avail.

As of right now it sends me to xxx@xxx.xxx.xxx:~/myname and then opens bash.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • The variable `${GRND}` is being expanded when you define the alias, not when you execute it, because the alias definition is in double quotes, not single quotes. I suggest you use a function instead of an alias. – Barmar Jan 28 '16 at 05:32

2 Answers2

2

This works for me;

alias zxc="ssh -t root@cal00 \"cd myname/\"${GRND}\"; exec bash\""

testing on my systems;

[~/w/git/qradar/packaging/patch/installer]$ zxc
[root@cal00 installer]# exit

So just a matter of replacing ' with \" single quotes prohibit variable substitution.

Cheers

Calvin Taylor
  • 664
  • 4
  • 15
  • 1
    I would suggest changing `bash` to `exec bash`, to save a process... But not very critical. – anishsane Jan 28 '16 at 06:27
  • While this worked, it's apparent that the GRND variable is being set at log in and doesn't follow me as I switch directories – SpaceCadet Jan 28 '16 at 17:56
2

$GRND is just being set once, when you login, it doesn't get updated whenever you change directories. Also, since you have the alias definition in double quotes, the variable is being expanded when your .bashrc runs, not when you execute the alias.

You should use a shell function instead.

zxc() {
    GRND=$(cut -d/ -f8- <<< "$PWD")
    ssh -t xxx@xxx.xxx.xxx "cd myname/\"$GRND\"; bash"
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Just to cross the last 't'; to get it to run everytime he changes directories would you make a new alias for cd? – Calvin Taylor Jan 28 '16 at 13:21
  • There's no need. The function sets `GRND` when you use it, based on the current directory. – Barmar Jan 28 '16 at 15:11
  • This is absolutely the correct answer. I tried it out and you're correct: a variable inside of an alias is always set at log in while a function will set the variable upon being invoked. This was a distinction I didn't understand before. – SpaceCadet Jan 28 '16 at 17:53
  • An alias will use the current value of the variable if you put the definition in single quotes. See http://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Barmar Jan 28 '16 at 17:57
  • But you still need to arrange for the variable to get updated automatically. You can use the `PROMPT_COMMAND` environment variable for this. – Barmar Jan 28 '16 at 18:05