1

I'm trying to assign container names to an array.

The command works perfectly when I'm running it without assigning to an array:

ARR=docker network inspect --format '{{ range $key, $value := .Containers }}{{ printf "%s\n" $value.Name}}{{ end }}' some_network $$ echo $ARR 

I'd like to do same thing but over SSH:

ssh [email protected] " ARR=( $(docker network inspect --format '"'{{ range $key, $value := .Containers }}{{ printf "%s\n" $value.Name}}{{ end }}'"' some_network) ) && echo $ARR " 

but it complains:

Template parsing error: template: :1: unexpected unclosed action in command 

I guess the reason lies in string interpolation or escaping ', can u give any advice guys?

3
  • echo $ARR is equivalent to echo ${ARR[0]}, you'll only see the first element. Commented Sep 13, 2021 at 11:58
  • 1
    But the problem is that you have the command substitution and echo $ARR in a double-quoted string, so your local shell will expand those before launching ssh Commented Sep 13, 2021 at 12:01
  • 1
    Why are you assigning the ARR variable remotely? Do you want to manipulate the data on your machine or the remote? Commented Sep 13, 2021 at 12:03

1 Answer 1

4

Within double-quotes, the command substitution would be handled on the local end, and the local shell would parse the quoting inside.

If you want to run it on the remote, you need something like

ssh [email protected] ' ARR=( $(docker network inspect --format '\''{{ range $key, $value := .Containers }}{{ printf "%s\n" $value.Name}}{{ end }}'\'' some_network) ) && echo $ARR ' 

or you could maybe avoid the quoting hell by giving the commands via stdin instead:

ssh [email protected] <<'EOF' ARR=( $(docker network inspect --format '{{ range $key, $value := .Containers }}{{ printf "%s\n" $value.Name}}{{ end }}' some_network) ) && echo $ARR EOF 

or by having the whole script in a separate file (on the remote or local side), and running from that.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.