Invalid bash variable

I am writing a script for ssh in a list of machines and comparing a variable with a different value. I'm having a problem (I have a few workarounds, but at the moment I'm just wondering why this method does not work).

VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"`

($ I will be the hostname. Hosts trust, no password provided)

Example foo.txt file:

foo:123456:abcdef
bar:789012:ghijkl
baz:345678:mnopqr

I assume this is a quotation mark problem or \ need somewhere. I tried several methods (different quotes using $ () instead of `` etc.), but it seems that not everything is correct. My script is working correctly using the following:

VAR=`ssh $i "grep bar /local/foo.txt" | awk -F: '{print \$2}'`

As I said, just curiosity, any answer is appreciated.

, : awk , 2- . \, , , "print" .., - .

+5
2

sshd bash , bash , , script. :

VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"`

$2 , bash script . , awk ( , ), :

awk -F: /^bar/ {print $2} /local/foo.txt

bash $2 , :

awk -F: /^bar/ {print } /local/foo.txt

. , ? , bash , :

VAR1=`ssh $i localhost "echo awk -F: '/^bar/ {print \\\$2}' /local/foo.txt"`

, , , bash, , :

VAR1=`ssh $i localhost "echo awk -F: '/^bar/ {print \$2}' /local/foo.txt"`
echo VAR1: $VAR1

Execute it and see this output and see right away that it has removed $2:
VAR1: awk -F: /^bar/ {print } /local/foo.txt
+5

$(),

VAR=$(ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt")
0
source

All Articles