jreisinger jreisinger - 1 year ago 41
Perl Question

Shell variable visible in double quoted Perl one-liner

I understand shell variables are local to the current shell while environment variables (the

ed ones) are passed onto child processes forked by the shell.

When I run a Perl one-liner within double quotes I can access the (local) shell variable from the
ed perl process:

$ FOO=bar
$ perl -we "print qx'echo $FOO'"

Why is that?

Answer Source

It's because of shell variable interpolation in double-quoted strings.

The $FOO variable is evaluated in the parent shell - in which $FOO has value bar - and interpolated into the quoted string.

Therefore the perl code you are actually running is:

print qx'echo bar'