1

In the terminal, I can access variable $LINES:

$ echo $LINES
39

Running Perl script like so:

 #!/usr/bin/env perl

 use strict; use warnings;

 my $cmd = q|echo $LINES|;
 my $lines = `$cmd`;

 print "lines: $lines\n";

gives output: lines:. I tried also accessing %ENV, but it does not contain this particular key.

How could I access shell variable $LINES from a Perl script?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
w.k
  • 8,218
  • 4
  • 32
  • 55

1 Answers1

4

From bash manual:

When a program is invoked it is given an array of strings called the environment. [...] The shell provides several ways to manipulate the environment. On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment. The export and declare -x commands allow parameters and functions to be added to and deleted from the environment.

So (assuming a Bash shell) using:

export LINES

will make the variable $LINES available from within a Perl script startet from the Shell (using $ENV{LINES} from the Perl script).

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174