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?

4
  • Have you tried export $LINES ? Commented Jan 14, 2020 at 13:11
  • No, but it should work, cause I tried LINES=20 my_script.pl and it works Commented Jan 14, 2020 at 13:19
  • stackoverflow.com/questions/1780483/… Commented Jan 14, 2020 at 13:20
  • @w.k : LINES=20 my_script.pl is equivalent to (export LINES=20; my_script.pl). You can't see non-exported variables. Commented Jan 14, 2020 at 14:12

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.