1

I am running below command on Bash prompt:

bash-3.2$ x=12
bash-3.2$ echo $x
12
bash-3.2$ perl -e '$age=$x; print "Age = $age\n"'
Age =
bash-3.2$

I am not getting the age/number printed..! How shall i import my unix bash variable inside my perl command..!?

3 Answers 3

6

First you have to export x in the shell. Then you can access the variable from Perl as $ENV{x}.

$ x=12
$ export x
$ perl -e '$age=$ENV{x}; print "Age = $age\n"'
Age = 12
Sign up to request clarification or add additional context in comments.

2 Comments

Or, you can use temporary assignment x=12 perl -e '$age = $ENV{x}; print "Age = $age\n"'.
Good answer ++. Have acknowledged on my alternative
1

This answer on this thread accesses the variable directly through the environment, which seems like a neater way.

Still, to demonstrate a way to use direct shell substitution (which has it's uses), then the right way to do it would be like this:

perl -e '$age='"$x"'; print "Age = $age\n"'
  • perl sees this as it's input: perl -e $age=12; print "Age = $age\n"

Comments

0

The single quotes for the -e parameter prevents the shell variable from being expanded. Use $ENV{'myvar'} to get the value of shell variable $myvar

And export the variable too as noted by the other answer.

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.