1

I want to get the home directory of a GCP instance by

p=$(gcloud compute ssh INSTANCE_NAME -- 'echo ${HOME}')

Suppose the p equals to /home/abc. Then I do a simple str concat and echo with

echo ${p}/xx

I would naturally expect

/home/abc/xx

Instead, it returns

/xxme/abc

Interesting enough, later chars xx displaces previous ones ho. How come? Any one with the fix?

user1584887
  • 277
  • 1
  • 11

1 Answers1

1

I address it by using the dos2unix solution in the question comment:

p=$(gcloud compute ssh INSTANCE_NAME -- 'echo ${HOME}' | dos2unix)

, despite the extra dependency.

user1584887
  • 277
  • 1
  • 11
  • 2
    Another option that will _probably_ work if you don't want to install `dos2unix` is to replace `dos2unix` with `tr -d '\r'` - that is, remove all CR characters in the stream. – Ted Lyngmo Feb 04 '22 at 13:25