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?

1
  • Are the variables environment variables or shell variables? If they are shell variables you won't be able to access them from Perl. Commented Jun 17, 2015 at 17:32

2 Answers 2

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

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

Comments

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

1 Comment

or, use single quotes: system('echo $user')

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.