0

In shell, I had sourced .cshrc file which contains some defined variables like user name.

I need to pass these variables to a certain Perl script.

For example in shell terminal, I typed

>echo $user

The output is >esaad

Then in Perl, to read $user variable, I tried:

system("echo $user")

Also tried this command:

my $userName = system( echo $ENV{user} );

but Perl asked for $user initialization as Perl variable not Shell one.

How I could read this variable?

chepner
  • 497,756
  • 71
  • 530
  • 681
eslam saad
  • 73
  • 1
  • 7
  • Are the variables environment variables or shell variables? If they are shell variables you won't be able to access them from Perl. – Diab Jerius Jun 17 '15 at 17:32

2 Answers2

4

You can:

print $ENV{'user'}

the reason your system call doesn't work is that system open a new shell that doesn't source .cshrc read this answer for more information

Community
  • 1
  • 1
Alex G
  • 64
  • 5
0

Either use Perl built-in system variable $ENV:

print $ENV{'user'}

Or use backslash to escape the variable $user. Perl will interpret for $user variable defined inside the Perl program without the backslash. With backslash, "echo $user" is passed as system call.

system("echo \$user")
Lye Heng Foo
  • 1,779
  • 1
  • 10
  • 8