19

How to use shell variables in perl command call in a bash shell script?

I have a perl command in my shell script to evaluate date -1.

How can i use $myDate in perl command call?

This is the section in my script:

myDate='10/10/2012'

Dt=$(perl -e 'use POSIX;print strftime '%m/%d/%y', localtime time-86400;")

I want use $myDate in place of %m/%d/%y.

Any help will be appreciated.

Thank you.

1
  • 1
    Replacing '%m/%d/%y' with the value of myDate doesn't make any sense unless you change the value of myDate to something like %m/%d/%y. Please fix your question appropriately. Commented Oct 26, 2012 at 20:29

5 Answers 5

37

Variables from the shell are available in Perl's %ENV hash. With bash (and some other shells) you need to take the extra step of "exporting" your shell variable so it is visible to subprocesses.

mydate=10/10/2012
export mydate
perl -e 'print "my date is $ENV{mydate}\n"'
Sign up to request clarification or add additional context in comments.

3 Comments

This is a simple answer that rules. The one above just looks like some convoluted complex code that I can't understand
Also, if you run perl -p, then it interprets the -- as a filename, so it is only possible to use $ENV in such scripts
@henley-chiu : In fairness to @ikegami's accepted answer, using the "-- arg" approach avoids polluting the bash environment, whereas using export in a bash function or alias means that running a function/alias with export will result in "mydate" being set for the duration of a bash session.
9

Using " instead of ' also passes shell variables to perl in version 5.24.

mydate=22/6/2016
perl -e "print $mydate"

1 Comment

This is what Ikegami recommends against in the accepted answer, for good reason. If you have complete control over mydate and understand what you are doing, interpolating it as Perl code is fine ... but that is certainly not true in this case, and so this snippet will produce erratic output 0.00181878306878307.
6

The same way you pass values to any other program: Pass it as an arg. (You might be tempted to generate Perl code, but that's a bad idea.)

Dt=$( perl -MPOSIX -e'print strftime $ARGV[0], localtime time-86400;' -- "$myDate" )

Note that code doesn't always return yesterday's date (since not all days have 86400 seconds). For that, you'd want

Dt=$( perl -MPOSIX -e'my @d = localtime time-86400; --$d[4]; print strftime $ARGV[0], @d;' -- "$myDate" )

or

Dt=$( perl -MDateTime -e'print DateTime->today(time_zone => "local")->subtract(days => 1)->strftime($ARGV[0]);' -- "$myDate" )

or simply

Dt=$( date --date='1 day ago' +"$myDate" )

1 Comment

This post assumes you want to use myDate as the format specifier as you said, which means that myDate actually contains something like %m/%d/%y.
3

For me, I need to use $ENV{'VARIABLE'} to pass VARIABLE in shell to Perl. I do not know why simply $ENV{VARIABLE} did not work.

For example,

In Bash:

#!/bin/bash -l
VARIABLE=1
export VARIABLE
perl example.pl

In Perl:

#!/usr/bin/perl -w
print "my number is " . $ENV{'VARIABLE'}

Comments

2

Why not something like: $ENV{'PATH'} = $ENV{'PATH'}.":"."/additional/path";

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.