-1

With double quotes

The following command

$ cat ./multi_meta | jq .Partitions[].DocCount | perl -lne "$x += $_; END{ print $x;}"

gives me a syntax error:

syntax error at -e line 1, near "+="
Execution of -e aborted due to compilation errors.

With single quotes

I get the correct result with

$ cat ./multi_meta | jq .Partitions[].DocCount | perl -lne '$x += $_; END{ print $x;}'

Why?

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30

2 Answers2

2

Because with double quotes, the whole string is passed through your shell's variable expansion mechanisms before the Perl compiler sees the code. And as you don't have shell variables called $x or $_, the Perl compiler sees this:

+= ; END{ print ;}

With single quotes, your Perl variables are protected from expansion until the Perl compiler can see them.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

I think the problem is your shell expanding $x as it was a shell variable and not as a perl variable, the solution should be escape $ like this \$.

sergiotarxz
  • 520
  • 2
  • 14