简体   繁体   中英

Different results when running shell command via ssh CLI vs. ssh shell

I have some baffling behavior that I'm sure has a logical explanation, but I'm wasting a lot of time trying to figure out.

Basically I'm getting different results for lsb_release -cs when run in different contexts against a Debian compute engine instance.

For example:

$ gcloud compute ssh instance-1 --command "lsb_release -cs"
bullseye
$ gcloud compute ssh instance-1 --command "echo $(lsb_release -cs)"
focal
$ gcloud compute ssh instance-1
user@instance-1:~$ lsb_release -cs
bullseye
user@instance-1:~$ echo $(lsb_release -cs)
bullseye

What in the world? For some reason using $(lsb_release -cs) passed via --command yields focal , but everywhere else it yields bullseye .

"$(lsb_release -cs)" was being evaluated by the local shell before ssh executed it.
Solution was to switch to single quotes: gcloud compute ssh instance-1 --command 'echo $(lsb_release -cs)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM