0

I have a running instance my-instance running on google-cloud. I run the following code on my local machine:

gcloud compute ssh my-instance --command 'pwd'
gcloud compute ssh my-instance --command 'cd ..'
gcloud compute ssh my-instance --command 'pwd'

my output is:

/home/pal
/home/pal

My expectation is:

/home/pal
/home

It seems that cd .. is not working. Why is it? It is surprising for me especially that pwd works as shown above. How can I solve the issue?


I used this page as source: https://cloud.google.com/sdk/gcloud/reference/compute/ssh

zabop
  • 6,750
  • 3
  • 39
  • 84
  • 2
    When you run `cd` in a new shell, the changed directory only lives as long as the shell does. Shell is started -> shell successfully runs `cd ..` -> she shell exits -> the parent process is still in whatever directory it was in before. So `cd ..` *works perfectly well*; it just doesn't impact completely separate commands' behavior. – Charles Duffy Dec 05 '18 at 18:57
  • 1
    Try `gcloud compute ssh my-instance --command 'cd .. && pwd'` – Charles Duffy Dec 05 '18 at 18:58
  • 2
    This isn't gcloud-specific in any way, btw -- you'd have the same behavior with any other `ssh somehost 'cd ..'` command. – Charles Duffy Dec 05 '18 at 18:58
  • 1
    See https://stackoverflow.com/questions/31958954/why-would-ssh-commands-through-putty-work-differently-to-those-via-phps-phpsecl for an example of the same issue being hit by someone else (using a completely different toolkit -- PHP's ssh client library instead of gcloud tools). – Charles Duffy Dec 05 '18 at 19:01
  • 1
    `gcloud compute ssh my-instance --command 'cd .. && pwd'` works as suggested by @Charles Duffy above. Thanks! – zabop Dec 05 '18 at 19:07
  • The clear description of the process by @CharlesDuffy explains what really happens. If you could post it an an Answer here, others will also benefit from that. Thanks. – mehdi sharifi Dec 26 '18 at 01:15
  • @mehdisharifi, ...I've added a set of links to duplicates (of other instances of the same question -- none of those using `gcloud ssh`, but all with the same underlying problem, and all with the same effective explanation). – Charles Duffy Dec 26 '18 at 14:55

0 Answers0