0

I just wanted to know how can I send variable that are set to bash script to Perl script. For Example

bash script

#!/bin/bash

var="bob"
perl perlscript.pl

return 0

so when the perlscript is launched I want to have bob in Perl script

#!/usr/bi/perl -w
print ""

3 Answers 3

3

ok after some reading the books I got this I think

#!/bin/bash

var="bob"
export var
perl perlscript.pl

and in perlscript.pl

#!/usr/bi/perl -w

my $var1 = $ENV{"var"};
print "\ $var1\n";

who knew book was actually more useful than internet

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

1 Comment

So you went with the environment variable rather than actually passing it as an argument to your Perl script then? By passing it as an argument to perlscript.pl you make it more portable and less reliant on an Environment variable being set. And you accepted your own answer. WOW !!
2

Since you want to access environment, you can use %ENV global hash,

print $ENV{var};

3 Comments

I get error saying use of uninitialized value in print at perlscript.pl line2.
@user3250974 var="bob"; perl -E 'say $ENV{var}'
I'm doing exact thing you are saying but I still get the same error
0

Might an easier way be to do the following...

Shell script

#!/bin/bash

var="bob"
perl perlscript.pl $var

Perl script

#!/usr/bin/perl

use strict;    

my $var = shift;    # reads first arg passed on command line
print "$var \n";

I suggest this as reading from the environment is ok but there is always a chance that you may read/write from/to an existing variable.

At least if you keep it to local shell script variables then you are in control of what is being passed into your Perl script.

1 Comment

oh yes this is what i was looking for. one of my friend told me this method but i forgot so i came here but they told me to use use env now i will use this.

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.